• S6:组合模式 Composite


    将对象组合成树形结构以表示整体-部分的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.

    UML:

    示例代码:
    透明组合:叶节点和子节点具有相同的接口

    abstract class Component
    {
        protected $name;
    
        public function __construct($name)
        {
            $this->name = $name;
        }
    
        abstract public function add(Component $node);
        abstract public function remove(Component $node);
        abstract public function display($deep);
    }
    
    // 页节点
    class Leaf extends Component
    {
        public function add(Component $node)
        {
            // 叶不能添加节点
        }
    
        public function remove(Component $node)
        {
            // 叶不能删除节点
        }
    
        public function display($deep)
        {
            echo str_repeat('-', $deep) . $this->name . PHP_EOL;
        }
    
    }
    
    // 枝节点
    class Composite extends Component
    {
        protected $nodes = array();
    
        public function add(Component $node)
        {
            $this->nodes[] = $node;
        }
    
        public function remove(Component $node)
        {
            unset($this->nodes[array_search($node, $this->nodes)]);
        }
    
        public function display($deep)
        {
            echo str_repeat('-', $deep) . $this->name . PHP_EOL;
    
            foreach ($this->nodes as $node)
            {
                $node->display($deep + 2);
            }
        }
    
    }
    
    
    $root = new Composite('/root');
    $root->add(new Leaf('/a.txt'));
    $root->add(new Leaf('/b.txt'));
    
    $etc = new Composite('/etc');
    $etc->add(new Leaf('httpd'));
    $etc->add(new Leaf('nginx'));
    
    $root->add($etc);
    $root->display(2);
    

      

    示例代码:
    安全组合:接口中不强制实现增加和删除节点,叶节点不具备该两项功能.

    abstract class Component
    {
        protected $name;
    
        public function __construct($name)
        {
            $this->name = $name;
        }
    
        abstract public function display($deep);
    }
    
    // 页节点
    class Leaf extends Component
    {
        public function display($deep)
        {
            echo str_repeat('-', $deep) . $this->name . PHP_EOL;
        }
    
    }
    
    // 枝节点
    class Composite extends Component
    {
        protected $nodes = array();
    
        public function add(Component $node)
        {
            $this->nodes[] = $node;
        }
    
        public function remove(Component $node)
        {
            unset($this->nodes[array_search($node, $this->nodes)]);
        }
    
        public function display($deep)
        {
            echo str_repeat('-', $deep) . $this->name . PHP_EOL;
    
            foreach ($this->nodes as $node)
            {
                $node->display($deep + 2);
            }
        }
    
    }
    
    
    $root = new Composite('/root');
    $root->add(new Leaf('/a.txt'));
    $root->add(new Leaf('/b.txt'));
    
    $etc = new Composite('/etc');
    $etc->add(new Leaf('httpd'));
    $etc->add(new Leaf('nginx'));
    
    $root->add($etc);
    $root->display(2);
    

      

  • 相关阅读:
    java获取当前类的路径
    oracle 报错 column ambiguously defined
    各种在线api地址
    双击打开我的电脑 看不到硬盘盘符出现慢 安装百度云之后我的电脑卡
    Tomcat下获取当前类的路径中含有空格的解决方案
    win8解压版Tomcat startup.bat一闪而过的解决办法
    Shell基础篇shell简介之bash编程之参数说明
    Shell 基础篇介绍几个shell程序设计小知识
    Oracle数据仓库的分层管理器解决方案
    DataStage在RedHat Linux Enterprise 3上安装详细步骤(三)
  • 原文地址:https://www.cnblogs.com/itfenqing/p/7788130.html
Copyright © 2020-2023  润新知