定義:
組合模式(Composite):將物件組合成樹形結構以表示“部分-整體”的層次結構,組合模式使得用戶對單個物件和組合物件的使用具有一致性,當你發現需求中是體現部分與整體層次的結構時,以及你希望用戶可以忽略組合物件與單個物件的不同,統一的使用組合結構中的所有物件時,就應該考慮用組合模式了,
實作方式:
1、透明方式:葉節點和枝節點對于外界沒有什么區別,它們具備完全一致的行為介面,問題則是葉節點會有冗余方法,
2、安全方式:葉節點中的冗余代碼不實作,問題則是由于不夠透明,所以葉節點和枝節點將不具有相同的介面,客戶端的呼叫需要做相應的判斷,帶來了不便,
代碼實體:
一、透明方式實作:
// 1、抽象類Component.php /** * 包含葉節點和枝節點方法的抽象類 * Class Component */ abstract class Component { /** * @var */ protected $name; /** * Component constructor. * @param $name */ public function __construct($name) { $this->name = $name; } /** * 添加葉節點或枝節點 * @param Component $component * @return mixed */ abstract public function add(Component $component); /** * @param $depth * @return mixed */ abstract public function display($depth); } // 2、枝節點Composite.php /** * 枝節點 * Class Composite */ class Composite extends Component { /** * @var array */ protected $children = []; /** * @param Component $component * @return mixed|void */ public function add(Component $component) { // TODO: Implement add() method. $this->children[] = $component; } public function display($depth) { // TODO: Implement display() method. $nameStr = str_repeat('-', $depth) . $this->name . '<br>'; foreach ($this->children as $component) { $nameStr .= $component->display($depth + 2); } return $nameStr; } } // 3、葉節點Leaf.php /** * 葉節點 * Class Leaf */ class Leaf extends Component { /** * 葉節點不需要添加子節點,但為了保持葉節點和枝節點一致,代碼冗余,透明方式 * @param Component $component * @return mixed|string */ public function add(Component $component) { // TODO: Implement add() method. return '葉節點不能添加子節點' . '<br>'; } public function display($depth) { // TODO: Implement display() method. return str_repeat('-', $depth) . $this->name . '<br>'; } }
呼叫:
// 生成樹根root,根上長出兩葉LeafA和LeafB $root = new Composite("root"); $root->add(new Leaf("Leaf A")); $root->add(new Leaf("Leaf B")); // 根上長出分支 CompositeX,分支上也有兩葉LeafXA和LeafXB $comp = new Composite("Composite X"); $comp->add(new Leaf("Leaf XA")); $comp->add(new Leaf("Leaf XB")); $root->add($comp); // 在CompositeX分支上再長出分支CompositeXY,分支上也有兩葉LeafXYA和LeafXYB $comp2 = new Composite("Composite XY"); $comp2->add(new Leaf("Leaf XYA")); $comp2->add(new Leaf("Leaf XYB")); $comp->add($comp2); echo $root->display(2);
結果:
--root ----Leaf A ----Leaf B ----Composite X ------Leaf XA ------Leaf XB ------Composite XY --------Leaf XYA --------Leaf XYB
二、安全方式實作:
// 1、抽象類Component.php /** * 包含葉節點和枝節點方法的抽象類 * Class Component */ abstract class Component { /** * @var */ protected $name; /** * Component constructor. * @param $name */ public function __construct($name) { $this->name = $name; } /** * @param $depth * @return mixed */ abstract public function display($depth); } // 2、枝節點Composite.php /** * 枝節點 * Class Composite */ class Composite extends Component { /** * @var array */ protected $children = []; /** * @param Component $component * @return mixed|void */ public function add(Component $component) { // TODO: Implement add() method. $this->children[] = $component; } public function display($depth) { // TODO: Implement display() method. $nameStr = str_repeat('-', $depth) . $this->name . '<br>'; foreach ($this->children as $component) { $nameStr .= $component->display($depth + 2); } return $nameStr; } } // 3、葉節點Leaf.php /** * 葉節點 * Class Leaf */ class Leaf extends Component { public function display($depth) { // TODO: Implement display() method. return str_repeat('-', $depth) . $this->name . '<br>'; } }
呼叫:
// 生成樹根root,根上長出兩葉LeafA和LeafB $root = new Composite("root"); $root->add(new Leaf("Leaf A")); $root->add(new Leaf("Leaf B")); // 根上長出分支 CompositeX,分支上也有兩葉LeafXA和LeafXB $comp = new Composite("Composite X"); $comp->add(new Leaf("Leaf XA")); $comp->add(new Leaf("Leaf XB")); $root->add($comp); // 在CompositeX分支上再長出分支CompositeXY,分支上也有兩葉LeafXYA和LeafXYB $comp2 = new Composite("Composite XY"); $comp2->add(new Leaf("Leaf XYA")); $comp2->add(new Leaf("Leaf XYB")); $comp->add($comp2); echo $root->display(2);
結果:
--root ----Leaf A ----Leaf B ----Composite X ------Leaf XA ------Leaf XB ------Composite XY --------Leaf XYA --------Leaf XYB
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/12583.html
標籤:設計模式
上一篇:【設計模式】單例模式
下一篇:PHP設計模式—裝飾器模式
