组合模式 - 结构模式

个人理解:   


模式类型:
    Composite 组合模式:结构型模式

意图:   

    The intent of this pattern is to compose objects into tree structures to represent part-whole hierarchies.   
    Composite lets clients treat individual objects and compositions of objects uniformly.
    允许你将对象组合成树形来表达结构来表现“整体/部分”层次结构。组合能让用户以一致的方式处理个别对象及对象组合。
    包含其他组件的组件为组合对象;不包含其他组件的组件为叶节点对象。


概述:

    组合模式(Composite Pattern)有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念  ,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。组合模式让你可以优化处理递归或分级数据结构。


角色:
    1、Component类:组合中的对象声明接口,在适当情况下,实现所有类共有接口的行为。声明一个接口用于访问和管理Component的子部件
    2、Leaf类:叶节点对象,叶节点没有子节点。由于叶节点不能增加分支和树叶,所以叶节点的Add和Remove没有实际意义。有叶节点行为,用来存储叶节点集合.
    3、Composite类:实现Componet的相关操作,比如Add和Remove操作。
    4、children:用来存储叶节点集合
    
模式的应用场景:
    引用大话设计模式的片段:“当发现需求中是体现部分与整体层次结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑组合模式了。
    
结构图:

模式的优缺点:

    1、定义了包含基本对象和组合对象的类层次结构,基本对象可以被组合成更复杂的组合对象,而这个组合对象有可以被组合。
    2、简化客户代码,客户可以一直地使用组合结构和单个对象,通常用户不知道处理的是一个叶节点还是一个组合组件。
    3、使得更容易增加新类型的组件, 新定义的Composite或leaf子类自动地与已有的结构和客户代码一起工作,客户程序不需要因为新的Component类而改变。
    4、使你的设计变得更一般化。
    
    组合模式为了保持”透明性“,常常会违反单一责任原则。也就是说,它一方面要管理内部对象,另一方面要提供一套访问接口
    当组合模式接口里提供删除子节点的方法时,在组件里有一个指向父节点的指针的话,实现删除操作会比较容易。

应用实例:
    在GUI里,我们常会发现一个顶层的组件(例如Frame、Panel)会包含其他组件(例如menu、checkbox、scrollbar),但是操作往往是同一套接口。在Android的View体系结构里就是这样,View其实就是Component,checkbox是Leaf,ViewGroup是Composite。
    
代码(其实读UML图要比代码还要一目了然)
/** 
 * 组件的接口 
 */  
public abstract class FileComponent {  
      
    public void add(FileComponent component){  
        throw new UnsupportedOperationException();  
    }  
      
    public void remove(FileComponent component){  
        throw new UnsupportedOperationException();  
    }  
      
    public FileComponent getChild(int index){  
        throw new UnsupportedOperationException();  
    }  
      
    public String getName(){  
        throw new UnsupportedOperationException();  
    }  
      
    public String getDescription(){  
        throw new UnsupportedOperationException();  
    }  
  
}  
  
/** 
 * 组合件的组件,可以包含组件或者叶节点 
 * @author sky 
 * 
 */  
public class FileComposite extends FileComponent {  
      
    private String name;  
      
    private List<FileComponent> list;  
      
    public FileComposite(String name){  
        this.name = name;  
        list = new ArrayList<>();  
    }  
  
    @Override  
    public void add(FileComponent component) {  
        list.add(component);  
    }  
  
    @Override  
    public void remove(FileComponent component) {  
        list.remove(component);  
    }  
  
    @Override  
    public FileComponent getChild(int index) {  
        if ( index>=0&&list.size()>index ) {  
            return list.get(index);  
        }  
        return null;  
    }  
  
    @Override  
    public String getName() {  
        return name;  
    }  
  
    @Override  
    public String getDescription() {  
        Iterator<FileComponent> iterator = list.iterator();  
        StringBuffer sb = new StringBuffer();  
        sb.append(name).append(":【");  
        while (iterator.hasNext()) {  
            sb.append(iterator.next().getDescription());  
        }  
        sb.append("】");  
        return sb.toString();  
    }  
  
}  
  
/** 
 * leaf叶节点 
 * @author sky 
 * 
 */  
public class FileLeaf extends FileComponent {  
      
    private String name;  
      
    public FileLeaf(String name){  
        this.name = name;  
    }  
  
    @Override  
    public String getName() {  
        return name;  
    }  
  
    @Override  
    public String getDescription() {  
        return "  name:"+name;  
    }  
  
}  
  
/** 
 * 测试代码 
 * @author sky 
 * 
 */  
public class Test {  
  
    public static void main(String[] args) {  
        FileComponent directory = new FileComposite("目录1");  
        FileComponent directory2 = new FileComposite("目录2");  
        FileComponent file = new FileLeaf("book");  
        directory2.add(file);  
        file = new FileLeaf("mp3");  
        directory2.add(file);  
        directory.add(directory2);  
        System.out.println(directory.getDescription());  
    }  
  
}  
<span style="font-family:Microsoft YaHei;">//</span>输出:  
//    目录1:【目录2:【  name:book  name:mp3】】  



所有模式:
     创建型模式,共五种:工厂方法模式、抽象工厂模式单例模式建造者模式原型模式
结构型模式,共七种:适配器模式装饰器模式代理模式外观模式桥接模式组合模式享元模式
    行为型模式,共十一种:策略模式模板方法模式观察者模式迭代子模式责任链模式命令模式备忘录模式状态模式访问者模式中介者模式解释器模式
    补充模式:空对象模式


参考/转自:

    http://blog.csdn.net/lansuiyun/article/details/11910555
    http://blog.csdn.net/imyfriend/article/details/8714164
    http://blog.csdn.net/xsxxxsxx/article/details/7218221
    http://blog.csdn.net/jason0539/article/details/22642281
    
转载请注明:http://blog.csdn.net/paincupid/article/details/44178215
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值