1. 程式人生 > 其它 >設計模式專題(十五) ——組合模式

設計模式專題(十五) ——組合模式

設計模式專題(十五)——組合模式

(原創內容,轉載請註明來源,謝謝)

一、概述

組合模式(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

相關閱讀:

設計模式專題(十四)——介面卡模式

設計模式專題(十三) ——備忘錄模式

設計模式專題(十二)——狀態模式

設計模式專題(十一)——抽象工廠模式

設計模式專題(十)——觀察者模式

設計模式專題(九) ——外觀模式

設計模式專題(八) ——模板方法模式

設計模式專題(七)——建造者模式

設計模式專題(六)——原型模式

設計模式專題(五)——工廠方法模式

設計模式專題(四)——代理模式

設計模式專題(三)——裝飾模式

設計模式專題(二)——策略模式

設計模式專題(一)——面向物件的設計原則