设计模式专题(十五) ——组合模式

时间:2022-05-03
本文章向大家介绍设计模式专题(十五) ——组合模式,主要内容包括设计模式专题(十四)——适配器模式、设计模式专题(十三) ——备忘录模式、设计模式专题(十二)——状态模式、设计模式专题(十一)——抽象工厂模式、设计模式专题(十)——观察者模式、设计模式专题(九) ——外观模式、设计模式专题(八) ——模板方法模式、设计模式专题(七)——建造者模式、设计模式专题(六)——原型模式、设计模式专题(五)——工厂方法模式、设计模式专题(四)——代理模式、设计模式专题(三)——装饰模式、设计模式专题(二)——策略模式、设计模式专题(一)——面向对象的设计原则、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

设计模式专题(十五)——组合模式

(原创内容,转载请注明来源,谢谢)

一、概述

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

使用组合模式,会使类之间完全透明,每个类都具有同样的方法,当又能区分谁是父类、谁是子类,实现一个树状的类图。当希望可以忽略单个对象和组合对象的区别,就可以使用此方法来管理对象。

二、优点

组合模式包含基本对象和组合对象的层次结构,使得客户端可以统一使用组合结构和对象。

三、类图

使用组合模式后,各类的层次结构如下:

四、设计实现

1、父类Component

         abstractclass Component{
                   private$arrChilds;
                   private$parent;
                   private$name;
                   publicfunction __construct($name, Component $co = null){
         $this->parent =$co;
         $this->name =$name;
}
public function getName();//获取该节点的名字
         public function remove($name){}//移除节点
         public function add(Component $co){}//增加节点
         public function getChilds(){}//获取全部孩子节点
         public function getParents{}//获取全部父节点
         public function specificMethods($prop1=null,$prop2=null){}//每个类特定的操作
}

2、子节点类Composite

class Compositeextends Component{
         private $arrChilds;
         private $parent;
         private $name;
         public function __construct($name,Component $co){
                   $this->parent = $co;
                   $this->name = $name;
                   $this->arrChilds =array();
         }
         //根据名字删除子节点,该删除仅删除子节点和父节点的联系,并不会从内存中删除子节点
         public function remove($name){
                   $res = false;
                   if(empty($this->arrChilds)){
                            return $res;
                   }
                   $arr = $this->arrChilds;
                   for($i=;$i<count($arr);$i++){
                            if($name ==$arr[$i]->name){
                                     unset($this->arrChilds{$i});
                                     $res =true;
                                     break;
                            }
                   }
                   return $res;
         }
         //添加子节点
         public function add(Component $co){
                   array_push($this->arrChilds,$co);
         }
         //获取全部孩子节点,引用传递获取值
         public function getChilds(array&$res){
                   foreach($this->arrChildsas $child){
                            array_push($res,$child);
                            if(null !=$child->arrChilds && !empty($child->arrChilds)){
                                     $child->getChilds($res);//递归
                            }
                   }
                   return $res;
         }
         //获取父节点
         public function getParents(array&$res){
                   array_push($res,$this->parent);
                   if(null != $this->parent->parent&& !empty($this->parent->parent)){
                            $this->parent->getParents($res);
                   }
                   return $res;
         }
         //独特的方法
         public functionspecificMethods($prop1=null,$prop2=null){
                   //......
         }
}

3、叶子节点类

class Leafextends Component{
         private $arrChilds;
         private $parent;
         private $name;
         public function __construct($name,Component $co){
                   $this->parent = $co;
                   $this->name = $name;
                   $this->arrChilds = null;
         }       
         public function remove($name = null){
                   return null;
         }
         public function add(Component $co =null){
                   return null;
         }
         public function getChilds(array&$res = null){
                   return null;
         }
         //获取父节点
         public function getParents(array&$res){
                   array_push($res,$this->parent);
                   if(null !=$this->parent->parent && !empty($this->parent->parent)){
                            $this->parent->getParents($res);
                   }
                   return $res;
         }
         //独特的方法
         public functionspecificMethods($prop1=null,$prop2=null){
                   //......
         }       
}

——written by linhxx 2017.08.11

相关阅读:

设计模式专题(十四)——适配器模式

设计模式专题(十三) ——备忘录模式

设计模式专题(十二)——状态模式

设计模式专题(十一)——抽象工厂模式

设计模式专题(十)——观察者模式

设计模式专题(九) ——外观模式

设计模式专题(八) ——模板方法模式

设计模式专题(七)——建造者模式

设计模式专题(六)——原型模式

设计模式专题(五)——工厂方法模式

设计模式专题(四)——代理模式

设计模式专题(三)——装饰模式

设计模式专题(二)——策略模式

设计模式专题(一)——面向对象的设计原则