Android开发Android知识Android技术知识

组合模式

2017-05-05  本文已影响22人  程序员丶星霖

组合模式

定义

也叫合成模式,有时又叫做部分-整体模式,主要是用来描述部分与整体的关系。将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

英文定义:Compose objects into tree structures to represent part-whole hierarchies . Composite lets clients treat individual objects and compositions of objects uniformly .

组合模式的UML类图如下所示:

组合模式.jpg

上图中的角色如下:

组合模式的示例代码如下所示:

//抽象构件
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);
            }
        }
    }
}

优缺点

优点:

缺点:
-限制了接口的影响范围,与依赖倒置原则冲突。

使用场景:

注意事项:

扩展

1、透明的组合模式

透明模式的好处是它基本遵循了依赖倒置原则,方便系统进行扩展。示例代码如下:

//抽象构件
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();
    }
}
//树结构遍历
public class Client{
    //通过递归遍历树
    public static void display(Component c){
        for(Component c:root.getChildren()){
            if(c instanceof Leaf){//叶子节点
                c.doSomething();
            }else{//树枝节点
                display(c);
            }
        }
    }
}

欢迎大家关注我的微信公众号

我的微信公众号.jpg
上一篇 下一篇

猜你喜欢

热点阅读