组合模式属于结构型模式,其意图是将对象组合成树形结构以表示部分-整体的层次结构,composite模式使得用户对单个对象和组合对象的使用具有一致性。Composite模式的一个重要思想是递归组合,关键是一个抽象类,它既可以代表组合对象,又可以代表一个被组合的对象。经常在Java开发中遇到的容器就使用了组合模式,一方面组件可以放在容器中,另一方面容器也可以作为组件放在另外的容器中。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

实用性:      

l         你想表示对象的部分-整体层次结构

l         你希望用户忽略组合对象与单个对象的不同,用户同意地使用组合结构中的所有对象。

参与者:

       Component

              为组合中的对象声明接口。

              在适当的情况下,实现所有类共有接口的缺省行为。

              声明一个接口用于访问和管理Componebt 的子组件。

              在递归结构中定义一个接口,用于访问一个父组件,并在合适的情况下实现。

Leaf

       在组合中表示叶节点对象,叶节点对象没有子节点。

       在组合中定义图元对象的行为。

Composite

       定义有子部件的那些部件的行为

       存储子部件,

       Component接口中实现与子部件有关的操作。

Client

       通过Component接口操纵组合部件的对象。
其协作过程是:用户使用 Compoment 类接口与组合结构中的对象进行交互。如果接受者是一个叶节点,则直接处理请求。如果接受者是 Composite ,它通常将请求发送给他的字部件,在转发请求之前或者之后可能执行一些辅助操作。

在本例子中,没有什么特殊之处,所不同的是 Composite 可以容纳 Component 的子类对象,包括本身。例子本省也比较简单,平淡无味,单却可以在特定的场景能很好的解决特定的问题。

使用组合模式有如下的好处:

l         定义了饱含基本对象和组合对象的类层次结构 , 基本对象可以被组合成更复杂的组合对象,而这个组合对象有可以被组合。

l         简化客户代码 客户可以一直地使用组合结构和单个对象,通常用户不知道处理的是一个叶节点还是一个组合组件。

l         使得更容易增加新类型的组件, 新定义的 Composite leaf 子类自动地与义有的结构和客户代码一起工作,客户程序不需要因为新的 Component 类而改变。

l         使你的设计变得更一般化。

实现的代码:

       Component 接口:

       package composite;

public interface Component{

    public int getSize();

    public int getChildNum();

    public String getType();

    public String getName();

    public void add(Component c);

    public void remove(Component c);

}

Leaf 类:

package composite;

public class Leaf implements Component{

    private int size ;

    private String name ;

    public Leaf(String aName, int aSize){

       size = aSize;

       name = aName;

    }

    public String getName(){

       return name ;

    }

    public String getType(){

       return "Leaf" ;

    }

    public int getSize(){

       return size ;

    }

    public int getChildNum(){

       return 1;

    }

    public void add(Component c){

       System. err .println( "Not supported method!" );

    }

    public void remove(Component c){

       System. err .println( "Not supported method!" );

    }

}

 

Composite 类:

package composite;

public class Leaf implements Component{

    private int size ;

    private String name ;

    public Leaf(String aName, int aSize){

       size = aSize;

       name = aName;

    }

    public String getName(){

       return name ;

    }

    public String getType(){

        return "Leaf" ;

    }

    public int getSize(){

       return size ;

    }

    public int getChildNum(){

       return 1;

    }

    public void add(Component c){

       System. err .println( "Not supported method!" );

    }

    public void remove(Component c){

       System. err .println( "Not supported method!" );

    }

}

 

客户端代码:

       package composite;

public class Client{

    public static void main(String[] args){

       Component root = new Composite( "root" );

       Component composite = new Composite( "composite" );

       Component leaf1 = new Leaf( "leaf1" ,20);

       Component leaf2 = new Leaf( "leaf2" ,40);

       root.add(leaf1);

       composite.add(leaf2);

       root.add(composite);

       String str = "Leaf1's size is " +leaf1.getSize();

       str += "\nleaf2's size is " + leaf2.getSize();

       str += "\nComposite's size is " + composite.getSize();

       str += "\nRoot's size is " + root.getSize();

       System. out .println(str);

    }

}

总结:组合模式在 解决整体与部分 的问题中 应用比较广泛,一个组合也可以被看作 部分来处理,降低了处理问题的难度。