合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 组合模式 组合(Composite)模式:有时又叫作部分-整体模式将。将对象组合成树状层次结构,使用户对单个对象和组合对象具有一致的访问性。 **主要优点有:** 1. 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码; 2. 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”; **主要缺点是:** 1. 设计较复杂,客户端需要花更多时间理清类之间的层次关系; 2. 不容易限制容器中的构件; 3. 不容易用继承的方法来增加构件的新功能; 常见使用场景:如树形菜单、文件夹菜单、部门组织架构图、窗体程序中的简单控件与容器控件等 #### **模式的结构** 组合模式包含以下主要角色。 1. \*\* 抽象构件(Component)角色\*\*:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。 2. **树叶构件(Leaf)角色**:是组合中的叶节点对象,它没有子节点,用于实现抽象构件角色中 声明的公共接口。 3. **树枝构件(Composite)角色**:是组合中的分支节点对象,它有子节点。它实现了抽象构件角色中声明的接口,它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。 组合模式分为透明式的组合模式和安全式的组合模式。 **透明式的组合模式** ``` <pre class="calibre10">``` <span class="token">/** *透明式合成模式 *抽象构件 */</span> interface <span class="token4">Component</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">remove</span><span class="token3">(</span>Component $component<span class="token3">)</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 树枝构件 */</span> class <span class="token4">Composite</span> implements <span class="token4">Component</span> <span class="token3">{</span> private $_children_composites<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//返回自己的实例</span> public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $this<span class="token3">;</span> <span class="token3">}</span> <span class="token">// 示例方法,调用各个子对象的operation方法</span> public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_children_composites as $composite<span class="token3">)</span> <span class="token3">{</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//聚集管理方法 添加一个子对象</span> public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>_children_composites<span class="token3">[</span><span class="token3">]</span> <span class="token1">=</span> $component<span class="token3">;</span> <span class="token3">}</span> <span class="token">//聚集管理方法 删除一个子对象</span> public <span class="token5">function</span> <span class="token4">remove</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span> foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_children_composites as $key <span class="token1">=</span><span class="token1">></span> $row<span class="token3">)</span> <span class="token3">{</span> <span class="token5">if</span> <span class="token3">(</span>$component <span class="token1">==</span> $row<span class="token3">)</span> <span class="token3">{</span> <span class="token4">unset</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_children_composites<span class="token3">[</span>$key<span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">return</span> TRUE<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token5">return</span> FALSE<span class="token3">;</span> <span class="token3">}</span> <span class="token">// 聚集管理方法 返回所有的子对象</span> public <span class="token5">function</span> <span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>_children_composites<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 树叶构件 */</span> class <span class="token4">Leaf</span> implements <span class="token4">Component</span> <span class="token3">{</span> private $_name<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>$name<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>_name <span class="token1">=</span> $name<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> echo $this<span class="token1">-</span><span class="token1">></span>_name<span class="token3">.</span><span class="token2">"<br>"</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> <span class="token5">null</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> FALSE<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">remove</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> FALSE<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> <span class="token5">null</span><span class="token3">;</span><span class="token">//树叶构件无子节点</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">CompositePattern</span><span class="token3">{</span> public static <span class="token5">function</span> <span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token">// client </span> $leaf1 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">'first'</span><span class="token3">)</span><span class="token3">;</span> $leaf2 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">'second'</span><span class="token3">)</span><span class="token3">;</span> $composite <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$leaf1<span class="token3">)</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$leaf2<span class="token3">)</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">remove</span><span class="token3">(</span>$leaf2<span class="token3">)</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> CompositePattern<span class="token3">:</span><span class="token3">:</span><span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` **安全式的组合模式** ``` <pre class="calibre10">``` <span class="token">/** * *安全式合成模式 */</span> interface <span class="token4">Component</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//返回自己的实例</span> public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> class <span class="token4">Composite</span> implements <span class="token4">Component</span> <span class="token3">{</span> <span class="token">// 树枝组件角色</span> private $_composites<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $this<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_composites as $composite<span class="token3">)</span> <span class="token3">{</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span> <span class="token">//聚集管理方法 添加一个子对象</span> $this<span class="token1">-</span><span class="token1">></span>_composites<span class="token3">[</span><span class="token3">]</span> <span class="token1">=</span> $component<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">remove</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span> <span class="token">// 聚集管理方法 删除一个子对象</span> foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_composites as $key <span class="token1">=</span><span class="token1">></span> $row<span class="token3">)</span> <span class="token3">{</span> <span class="token5">if</span> <span class="token3">(</span>$component <span class="token1">==</span> $row<span class="token3">)</span> <span class="token3">{</span> <span class="token4">unset</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_composites<span class="token3">[</span>$key<span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">return</span> TRUE<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token5">return</span> FALSE<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getChild</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 聚集管理方法 返回所有的子对象</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>_composites<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">Leaf</span> implements <span class="token4">Component</span> <span class="token3">{</span> private $_name<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>$name<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>_name <span class="token1">=</span> $name<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> echo $this<span class="token1">-</span><span class="token1">></span>_name<span class="token3">.</span><span class="token2">"被访问了<br>"</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> <span class="token5">null</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">CompositePattern</span><span class="token3">{</span> public static <span class="token5">function</span> <span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token">// client</span> $leaf1 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">'first'</span><span class="token3">)</span><span class="token3">;</span> $leaf2 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">'second'</span><span class="token3">)</span><span class="token3">;</span> $composite <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$leaf1<span class="token3">)</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$leaf2<span class="token3">)</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> echo <span class="token2">"-----------<br />"</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">remove</span><span class="token3">(</span>$leaf2<span class="token3">)</span><span class="token3">;</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> CompositePattern<span class="token3">:</span><span class="token3">:</span><span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> 结果: first被访问了 second被访问了 <span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">-</span> first被访问了 ``` ``` ## **组合模式的应用场景** 1. 在需要表示一个对象整体与部分的层次结构的场合。 2. 要求对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合。 ``` <pre class="calibre10">``` <span class="token">//抽象构件</span> Abstract class <span class="token4">Component</span><span class="token3">{</span> public $name<span class="token3">;</span> abstract <span class="token5">function</span> <span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>$name<span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>name <span class="token1">=</span> $name<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//总经理 部门经理 主管等 树枝构件</span> class <span class="token4">Composite</span> extends <span class="token4">Component</span><span class="token3">{</span> public $lever <span class="token1">=</span> <span class="token6">1</span><span class="token3">;</span> public $c_nodes <span class="token1">=</span> <span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">""</span><span class="token3">;</span> <span class="token4">print_r</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>c_nodes<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">//添加子节点</span> public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span><span class="token3">{</span> $component<span class="token1">-</span><span class="token1">></span>lever <span class="token1">=</span> $this<span class="token1">-</span><span class="token1">></span>lever <span class="token1">+</span> <span class="token6">1</span><span class="token3">;</span> $this<span class="token1">-</span><span class="token1">></span>c_nodes<span class="token3">[</span><span class="token3">]</span> <span class="token1">=</span> $component<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> $nbsp<span class="token1">=</span><span class="token4">str_repeat</span><span class="token3">(</span><span class="token2">"&nbsp;"</span><span class="token3">,</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>lever<span class="token1">-</span><span class="token6">1</span><span class="token3">)</span><span class="token1">*</span><span class="token6">4</span><span class="token3">)</span><span class="token3">;</span> echo <span class="token2">"{$nbsp}我是层级{$this->lever}:{$this->name}<br>"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//普通员工 树叶构件 不能添加子节点</span> class <span class="token4">Leaf</span> extends <span class="token4">Component</span><span class="token3">{</span> public $lever<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> $nbsp<span class="token1">=</span><span class="token4">str_repeat</span><span class="token3">(</span><span class="token2">"&nbsp;"</span><span class="token3">,</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>lever<span class="token1">-</span><span class="token6">1</span><span class="token3">)</span><span class="token1">*</span><span class="token6">4</span><span class="token3">)</span><span class="token3">;</span> echo <span class="token2">"{$nbsp}层级{$this->lever}:{$this->name}(work)<br>"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $manager <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token2">"总经理"</span><span class="token3">)</span><span class="token3">;</span> $xgm <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token2">"销售经理"</span><span class="token3">)</span><span class="token3">;</span> $sgm <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token2">"行政经理"</span><span class="token3">)</span><span class="token3">;</span> $staff <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">"张三"</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//组装成树</span> $manager<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$xgm<span class="token3">)</span><span class="token3">;</span> $manager<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$sgm<span class="token3">)</span><span class="token3">;</span> $sgm<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$staff<span class="token3">)</span><span class="token3">;</span> $staff2 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">"李四"</span><span class="token3">)</span><span class="token3">;</span> $sgm<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$staff2<span class="token3">)</span><span class="token3">;</span> $staff3 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">"王五"</span><span class="token3">)</span><span class="token3">;</span> $xgm<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$staff3<span class="token3">)</span><span class="token3">;</span> $manager<span class="token1">-</span><span class="token1">></span><span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//遍历树 - 函数</span> <span class="token5">function</span> <span class="token4">display</span><span class="token3">(</span>Composite $composite<span class="token3">)</span><span class="token3">{</span> $composite<span class="token1">-</span><span class="token1">></span><span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">foreach</span><span class="token3">(</span>$composite<span class="token1">-</span><span class="token1">></span>c_nodes as $c_node<span class="token3">)</span><span class="token3">{</span> <span class="token">//属于枝叶执行doSomething 否则继续回调displa函数</span> $c_node <span class="token5">instanceof</span> <span class="token4">Leaf</span> <span class="token1">?</span> $c_node<span class="token1">-</span><span class="token1">></span><span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">:</span> <span class="token4">display</span><span class="token3">(</span>$c_node<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token4">display</span><span class="token3">(</span>$manager<span class="token3">)</span><span class="token3">;</span> ``` ``` 结果: ``` <pre class="calibre10">``` Array <span class="token3">(</span> <span class="token3">[</span><span class="token6">0</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Composite Object <span class="token3">(</span> <span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">2</span> <span class="token3">[</span>c_nodes<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Array <span class="token3">(</span> <span class="token3">[</span><span class="token6">0</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Leaf Object <span class="token3">(</span> <span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">3</span> <span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 王五 <span class="token3">)</span> <span class="token3">)</span> <span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 销售经理 <span class="token3">)</span> <span class="token3">[</span><span class="token6">1</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Composite Object <span class="token3">(</span> <span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">2</span> <span class="token3">[</span>c_nodes<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Array <span class="token3">(</span> <span class="token3">[</span><span class="token6">0</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Leaf Object <span class="token3">(</span> <span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">3</span> <span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 张三 <span class="token3">)</span> <span class="token3">[</span><span class="token6">1</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Leaf Object <span class="token3">(</span> <span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">3</span> <span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 李四 <span class="token3">)</span> <span class="token3">)</span> <span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 行政经理 <span class="token3">)</span> <span class="token3">)</span> 我是层级<span class="token6">1</span><span class="token3">:</span>总经理 我是层级<span class="token6">2</span><span class="token3">:</span>销售经理 层级<span class="token6">3</span><span class="token3">:</span>王五(work) 我是层级<span class="token6">2</span><span class="token3">:</span>行政经理 层级<span class="token6">3</span><span class="token3">:</span>张三(work) 层级<span class="token6">3</span><span class="token3">:</span>李四(work) ``` ``` 实例2: ![](https://img.kancloud.cn/5c/4d/5c4d2aab0b864b3819857f722ed23c7c_486x321.png) ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php <span class="token">//模型中所有的类都扩展自 Unit 类,同时,Army 和 TroopCarrier 类被设计成了组合对象,用于包含 Unit 对象。</span> <span class="token">//但是,我们也发现 ArcherUnit 和 LaserCannonUnit 类(称为局部对象,也称为树叶对象,因为组合模式为树形结构,组合对象为树干,单独存在的对象为树叶,树叶对象应为最小单元,其中不能包含其他对象。)不能包含其他 Unit 对象,但也实现了addUnit() 方法,这个问题我们后面讨论。</span> <span class="token">/* 战斗单元的抽象类 有一个抽象方法bombardStrength()方法用于设置战斗单元的攻击强度 */</span> abstract class <span class="token4">Unit</span><span class="token3">{</span> <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">null</span><span class="token3">;</span> <span class="token3">}</span> abstract <span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">//弓箭手</span> class <span class="token4">Archer</span> extends <span class="token4">Unit</span><span class="token3">{</span> <span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token6">4</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//激光炮单位</span> class <span class="token4">LaserCanonUnit</span> extends <span class="token4">Unit</span><span class="token3">{</span> <span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token6">44</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/* CompositeUnit 混合作战单元类 因为CompositeUnit没有实现bombardStrength()方法, 所以CompositeUnit声明为抽象类abstract 战斗单元为树叶类时为了避免使用addUnit与removeUnit则通过下面抽象出的CompositeUnit类的getComposite */</span> abstract class <span class="token4">CompositeUnit</span> extends <span class="token4">Unit</span><span class="token3">{</span> private $units<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $this<span class="token3">;</span> <span class="token3">}</span> protected <span class="token5">function</span> <span class="token4">units</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token3">;</span> <span class="token3">}</span> <span class="token5">function</span> <span class="token4">removeUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span> $units<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>units as $thisunit<span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span><span class="token1">!</span>unit<span class="token1">!==</span>$thisunit<span class="token3">)</span><span class="token3">{</span> $units<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$thisunit<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token1">=</span>$units<span class="token3">;</span> <span class="token3">}</span> <span class="token5">function</span> <span class="token4">addUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span><span class="token4">in_array</span><span class="token3">(</span>$unit<span class="token3">,</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token3">,</span><span class="token6">true</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span><span class="token3">;</span> <span class="token3">}</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$unit<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 定义一个独立的类(Army类)来组合战斗单元。 * 战斗集合的类 树枝类 * 如果 Army(军队)类需要和其他Army类进行合并,同时,每个 Army 都有自己的 ID,这样 Army 在以后还可以从整编中解散出来。 * 我们修改原来的 Army(军队)类,使其同时支持 Unit(单元)和 Army(军队)。 */</span> class <span class="token4">Army</span> extends <span class="token4">CompositeUnit</span><span class="token3">{</span> public $units<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//添加单位</span> <span class="token5">function</span> <span class="token4">addUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span><span class="token4">in_array</span><span class="token3">(</span>$unit<span class="token3">,</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token3">,</span><span class="token6">true</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span><span class="token3">;</span> <span class="token3">}</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$unit<span class="token3">;</span> <span class="token3">}</span> <span class="token">//移除单位</span> <span class="token5">function</span> <span class="token4">removeUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span> $units<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>units as $thisunit<span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span>$unit<span class="token1">!==</span>$thisunit<span class="token3">)</span><span class="token3">{</span> $units<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$thisunit<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token1">=</span>$thisunit<span class="token3">;</span> <span class="token3">}</span> <span class="token">//总攻击强度</span> <span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> $ret<span class="token1">=</span><span class="token6">0</span><span class="token3">;</span> foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>units as $unit<span class="token3">)</span><span class="token3">{</span> $ret<span class="token1">+</span><span class="token1">=</span>$unit<span class="token1">-</span><span class="token1">></span><span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">return</span> $ret<span class="token3">;</span> <span class="token3">}</span> <span class="token">//添加陆军</span> <span class="token5">function</span> <span class="token4">addArmy</span><span class="token3">(</span>Army $army<span class="token3">)</span><span class="token3">{</span> <span class="token4">array_push</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>armies<span class="token3">,</span> $army<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//运兵船</span> class <span class="token4">TroopCarrier</span> extends <span class="token4">CompositeUnit</span><span class="token3">{</span> <span class="token5">function</span> <span class="token4">addUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span>$unit <span class="token5">instanceof</span> <span class="token4">Cavalry</span><span class="token3">)</span><span class="token3">{</span><span class="token">//骑兵对象</span> <span class="token5">throw</span> <span class="token5">new</span> <span class="token4">UnitException</span><span class="token3">(</span><span class="token2">"不能将骑兵带上船"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> Parent<span class="token3">:</span><span class="token3">:</span><span class="token4">addUnit</span><span class="token3">(</span>$unit<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token6">0</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//骑兵对象</span> class <span class="token4">Cavalry</span> extends <span class="token4">CompositeUnit</span><span class="token3">{</span> <span class="token5">function</span> <span class="token4">addUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span> <span class="token3">}</span> <span class="token5">function</span> <span class="token4">removeUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span> <span class="token3">}</span> <span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">UnitException</span> extends <span class="token4">Exception</span><span class="token3">{</span> <span class="token3">}</span> class <span class="token4">UnitScript</span><span class="token3">{</span> <span class="token">//有两个Unit类型的参数,第一个是新的Unit对象,第二个是之前的Unit对象</span> static <span class="token5">function</span> <span class="token4">joinExisting</span><span class="token3">(</span>Unit $newUnit<span class="token3">,</span>Unit $oldUnit<span class="token3">)</span><span class="token3">{</span> $comp<span class="token3">;</span> <span class="token5">if</span><span class="token3">(</span><span class="token1">!</span><span class="token4">is_null</span><span class="token3">(</span>$comp<span class="token1">=</span>$oldUnit<span class="token1">-</span><span class="token1">></span><span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> <span class="token">//如果第二个Unit对象是一个CompositeUnit对象,那么直接add</span> $comp<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span>$newUnit<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span><span class="token5">else</span> <span class="token3">{</span> <span class="token">//如果第二个Unit对象不是一个CompositeUnit对象,那么创建一个Army对象,将两个Unit存入这个Army对象 </span> $comp<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $comp<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span>$oldUnit<span class="token3">)</span><span class="token3">;</span> $comp<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span>$newUnit<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">return</span> $comp<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $main_army<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $main_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> $main_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">LaserCanonUnit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> $sub_army<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $sub_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> $sub_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> $sub_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> $main_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span>$sub_army<span class="token3">)</span><span class="token3">;</span> $a<span class="token1">=</span><span class="token5">new</span> <span class="token4">TroopCarrier</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $b<span class="token1">=</span>$a<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//$b=$a->addUnit(new Cavalry());</span> <span class="token">//print_r($main_army->units);</span> print <span class="token2">"攻击强度:{$main_army->bombardStrength()}<br>"</span><span class="token3">;</span> <span class="token">//也可以这样 </span> UnitScript<span class="token3">:</span><span class="token3">:</span><span class="token4">joinExisting</span><span class="token3">(</span>$main_army<span class="token3">,</span> $sub_army<span class="token3">)</span><span class="token3">;</span> $main<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $main<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> $sub<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $sub<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">LaserCanonUnit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> $all<span class="token1">=</span>UnitScript<span class="token3">:</span><span class="token3">:</span><span class="token4">joinExisting</span><span class="token3">(</span>$main<span class="token3">,</span> $sub<span class="token3">)</span><span class="token3">;</span> print <span class="token2">"攻击强度:{$all->bombardStrength()}<br>"</span><span class="token3">;</span> ``` ``` 结果: ``` <pre class="calibre17">``` 攻击强度:<span class="token6">60</span> 攻击强度:<span class="token6">48</span> ``` ```