您的当前位置:首页>全部文章>文章详情

php设计模式之合成模式(Composite)

发表于:2019-08-16 10:27:44浏览:54次TAG:
组合模式(Composite Pattern)有时候又叫做部分-整体模式,用于将对象组合成树形结构以表示“部分-整体”的层次关系。组合模式使得用户对单个对象和组合对象的使用具有一致性。 常见使用场景:如树形菜单、文件夹菜单、部门组织架构图等。 ```php _composites = array(); } public function getComposite() { return $this; } // 示例方法,调用各个子类对象的operation()方法 public function operation() { foreach ($this->_composites as $key => $_composite) { $_composite->operation(); } } // 聚集管理方法 添加一个子对象 public function add(Component $component) { $this->_composites[] = $component; } // 聚集管理办法 删除一个子对象 public function remove(Component $component) { foreach ($this->_composites as $key => $_composite) { if($component == $_composite) { unset($this->_composites[$key]); return true; } } return false; } // 聚集管理办法 返回所有的子对象 public function getChild() { return $this->_composites; } } class Leaf implements Component { private $_name; public function __construct($name) { $this->_name = $name; } public function operation() { var_dump("叶子leaf[{$this->_name}]执行了operaiton()方法"); } public function getComposite() { return null; } } $leaf1 = new Leaf('first'); $leaf2 = new Leaf('second'); // 1.实例化 树枝组件角色 $composite = new Composite(); // 2.将 叶子 对象 加入到 树枝组件角色中 $composite->add($leaf1); $composite->add($leaf2); // 3.遍历 树枝组件角色中 所有的叶子,并执行叶子中的方法 $composite->operation(); // 输出: 叶子leaf[first]执行了operaiton()方法 // 输出: 叶子leaf[second]执行了operaiton()方法 // 4.遍历 树枝组件角色中 所有的叶,找到 叶子 并删除 $composite->remove($leaf2); // 5.再次遍历 树枝组件角色中 所有的叶子,并执行叶子中的方法 $composite->operation(); // 输出: 叶子leaf[first]执行了operaiton()方法 ?> ``` ```php _composites = array(); } public function getComposite() { return $this; } // 示例方法,调用各个子类对象的operation()方法 public function operation() { foreach ($this->_composites as $key => $_composite) { $_composite->operation(); } } // 聚集管理方法 添加一个子对象 public function add(Component $component) { $this->_composites[] = $component; } // 聚集管理办法 删除一个子对象 public function remove(Component $component) { foreach ($this->_composites as $key => $_composite) { if($component == $_composite) { unset($this->_composites[$key]); return true; } } return false; } // 聚集管理办法 返回所有的子对象 public function getChild() { return $this->_composites; } } class Leaf implements Component { private $_name; public function __construct($name) { $this->_name = $name; } public function operation() { var_dump("叶子leaf[{$this->_name}]执行了operaiton()方法"); } public function getComposite() { return null; } public function add(Component $component) { return false; } public function remove(Component $component) { return false; } public function getChild() { return null; } } // client $leaf1 = new Leaf('first'); $leaf2 = new Leaf('second'); // 1.实例化 树枝组件角色 $composite = new Composite(); // 2.将 叶子 对象 加入到 树枝组件角色中 $composite->add($leaf1); $composite->add($leaf2); // 3.遍历 树枝组件角色中 所有的叶子,并执行叶子中的方法 $composite->operation(); // 输出: 叶子leaf[first]执行了operaiton()方法 // 输出: 叶子leaf[second]执行了operaiton()方法 // 4.遍历 树枝组件角色中 所有的叶,找到 叶子 并删除 $composite->remove($leaf2); // 5.再次遍历 树枝组件角色中 所有的叶子,并执行叶子中的方法 $composite->operation(); // 输出: 叶子leaf[first]执行了operaiton()方法 ?> ``` 输出 ```bash vagrant@vagrant-ubuntu:~$ sudo php composite.php string(43) "叶子leaf[first]执行了operaiton()方法" string(44) "叶子leaf[second]执行了operaiton()方法" string(43) "叶子leaf[first]执行了operaiton()方法" ```
栏目分类全部>
腾讯云采购季云服务器一折促销