参考:http://terrylee.cnblogs.com/archive/2006/03/11/347919.html
树状结构中,对于枝节点和叶节点,接口不同,客户端需要判断处理的节点类型。
对于规模比较大的公司,其组织架构一般是:总公司,下面有分公司和直属部门(总公司的人事、财务、行政),分公司下面又有直属部门(分公司的人事、财务、行政)和支公司,支公司下面是各个部门。
在客户端程序中,需要判断返回对象的具体类型到底是公司还是部门。如果是公司,则需要递归处理。
这增加了客户端程序与复杂元素内部结构之间的依赖。组合模式可以解决这种问题。
组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。
组合模式模糊了树型结构问题中简单元素和复杂元素的概念,客户程序可以跟处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。
组合模式包含四个角色:
Component:对象声明接口,声明所有类共有的默认行为Composite:有子节点的节点,存储子节点并实现 Component 接口有关操作Leaf:叶节点,没有子节点,用于定义对象的基本行为Client:客户类组合模式可以不提供父对象的管理方法,但必须提供子对象的管理方法(add、remove 等)。
根据所实现接口的区别,组合模式包括两种:
透明组合模式:叶节点和枝节点接口统一。在 Component 中声明所有用来管理子对象的方法(Add、Remove 等)。这样实现 Component 接口的所有子类都具备了管理子对象的方法。问题很明显,因为 Leaf 类本身不具备管理子对象的功能,所以实现的部分方法没有意义。安全组合模式:叶节点和枝节点接口不统一。不在 Component 中声明用来管理子对象的方法,而是在 Composite 声明所有用来管理子类对象的方法。问题也很明显,叶节点和枝节点将不具有相同的接口,客户端需要进行判断。输出:
Composite Object ( [children:Composite:private] => Array ( [0] => Leaf Object ( [name:protected] => Leaf A ) [1] => Leaf Object ( [name:protected] => Leaf B ) [2] => Composite Object ( [children:Composite:private] => Array ( [0] => Leaf Object ( [name:protected] => Leaf XA ) [1] => Leaf Object ( [name:protected] => Leaf XB ) [2] => Composite Object ( [children:Composite:private] => Array ( [0] => Leaf Object ( [name:protected] => Leaf XYA ) [1] => Leaf Object ( [name:protected] => Leaf XYB ) ) [name:protected] => Composite XY ) ) [name:protected] => Composite X ) [3] => Leaf Object ( [name:protected] => Leaf C ) ) [name:protected] => root ) -1 root -depthLeaf A -depthLeaf B -3 Composite X -depthLeaf XA -depthLeaf XB -5 Composite XY -depthLeaf XYA -depthLeaf XYB -depthLeaf C转载于:https://www.cnblogs.com/kika/p/10851560.html
