组合模式

组合模式的定义

组合模式(Composite Pattern)也叫合成模式,有时又叫做部分-整体模式(Part-Whole),

主要是用来描述部分与整体的关系,其定义如下:

Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients

treat individual objects and compositions of objects uniformly.(将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。)

● Component抽象构件角色

定义参加组合对象的共有方法和属性,可以定义一些默认的行为或属性

● Leaf叶子构件

叶子对象,其下再也没有其他的分支,也就是遍历的最小单位。

● Composite树枝构件

树枝对象,它的作用是组合树枝节点和叶子节点形成一个树形结构。

抽象构件

public abstract class Component {

     //个体和整体都具有的共享

     public void doSomething(){

             //编写业务逻辑

     }

}

树枝构件

public class Composite extends Component {

     //构件容器

     private ArrayList<Component> componentArrayList = new ArrayList<Component>();

     //增加一个叶子构件或树枝构件

     public void add(Component component){

             this.componentArrayList.add(component);

     }

     //删除一个叶子构件或树枝构件

     public void remove(Component component){

             this.componentArrayList.remove(component);

     }

     //获得分支下的所有叶子构件和树枝构件

     public ArrayList<Component> getChildren(){

             return this.componentArrayList;

     }

}

树叶节点是没有子下级对象的对象,定义参加组合的原始对象行为

树叶构件

public class Leaf extends Component {

     /*

      * 可以覆写父类方法

      * public void doSomething(){

      *

      * }

      */

}

场景类负责树状结构的建立,并可以通过递归方式遍历整个树

场景类

public class Client {

     public static void main(String[] args) {

            //创建一个根节点

             Composite root = new Composite();

             root.doSomething();

             //创建一个树枝构件

             Composite branch = new Composite();

             //创建一个叶子节点

             Leaf leaf = new Leaf();

             //建立整体

             root.add(branch);

             branch.add(leaf);          

     }

     //通过递归遍历树

     public static void display(Composite root){

             for(Component c:root.getChildren()){

                  if(c instanceof Leaf){ //叶子节点

                          c.doSomething();

                  }else{ //树枝节点

                          display((Composite)c);

                  }

             }

     }

}

组合模式的优点

● 高层模块调用简单

一棵树形机构中的所有节点都是Component,局部和整体对调用者来说没有任何区别,

也就是说,高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的

代码。

● 节点自由增加

使用了组合模式后,我们可以看看,如果想增加一个树枝节点、树叶节点是不是都很容

易,只要找到它的父节点就成,非常容易扩展,符合开闭原则,对以后的维护非常有利。

组合模式的缺点

直接使用了实现类!它限制了你接口的影响范围。

组合模式的使用场景

● 维护和展示部分-整体关系的场景,如树形菜单、文件和文件夹管理。

● 从一个整体中能够独立出部分模块或功能的场景。

只要是树形结构,就要考虑使用组合模式

组合模式的扩展

 1.真实的组合模式

什么是真实的组合模式?就是你在实际项目中使用的组合模式,而不是仅仅依照书本上

学习到的模式,它是“实践出真知”。

2.透明的组合模式

组合模式有两种不同的实现:透明模式和安全模式

透明模式是把用来组合使用

的方法放到抽象类中,比如add()、remove()以及getChildren等方法(顺便说一下,getChildren一般返回的结果为Iterable的实现类,安全模式,它是把树

枝节点和树叶节点彻底分开,树枝节点单独拥有用来组合的方法,这种方法比较安全

透明模式通用源代码

抽象构件

public abstract class Component {

     //个体和整体都具有的共享

     public void doSomething(){

             //编写业务逻辑

     }

     //增加一个叶子构件或树枝构件

     public abstract void add(Component component);

     //删除一个叶子构件或树枝构件

     public abstract void remove(Component component);

     //获得分支下的所有叶子构件和树枝构件

     public abstract ArrayList<Component> getChildren();

}

树叶节点

public class Leaf extends Component {

     @Deprecated

     public void add(Component component) throws UnsupportedOperationException{

             //空实现,直接抛弃一个”不支持请求”异常

             throw new UnsupportedOperationException();

     }

     @Deprecated

     public void remove(Component component)throws UnsupportedOperationException{

             //空实现

             throw new UnsupportedOperationException();

     }

     @Deprecated

     public ArrayList<Component> getChildren()throws UnsupportedOperationException{

             //空实现

             throw new UnsupportedOperationException();          

     }

}

为什么要加个Deprecated注解呢?就是在编译器期告诉调用者,你可以调我这个方法,

但是可能出现错误哦,我已经告诉你“该方法已经失效”了,你还使用那在运行期也会抛出

UnsupportedOperationException异常。

在透明模式下,遍历整个树形结构是比较容易的,不用进行强制类型转换

 树结构遍历

public class Client {

     //通过递归遍历树

     public static void display(Component root){

             for(Component c:root.getChildren()){

                  if(c instanceof Leaf){ //叶子节点

                          c.doSomething();

                  }else{ //树枝节点

                          display(c);

                  }

          }

     }

}

仅仅在遍历时不再进行牵制的类型转化了,其他的组装则没有任何变化。透明模式的好

处就是它基本遵循了依赖倒转原则,方便系统进行扩展。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注