组合模式

描述

    组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。

简介

安全式组合模式

透明式组合模式

    安全模式的组合模式要求管理聚集的方法只出现在树枝构件类中,而不出现在树叶构件类中。
    透明式的组合模式要求所有的具体构件类,不论树枝构件还是树叶构件,均符合一个固定接口。

角色

  • 抽象构件(Component)角色:这是一个抽象角色,它给参加组合的对象定义出公共的接口及其默认行为,可以用来管理所有的子对象。组合对象通常把它所包含的子对象当做类型为Component的对象。在安全式的组合模式里,构件角色并不定义出管理子对象的方法,这一定义由树枝构件对象给出。
  • 树叶构件(Leaf)角色:树叶对象是没有下级子对象的对象,定义出参加组合的原始对象的行为。
  • 树枝构件(Composite)角色:代表参加组合的有下级子对象的对象。树枝构件类给出所有的管理子对象的方法,如add()、delete()以及get()。

优缺点

优点

  • 组合模式使得客户端代码可以一致地处理对象和对象容器,无需关系处理的单个对象,还是组合的对象容器。
  • 可以更容易地往组合对象中加入新的构件。
  • 将”客户代码与复杂的对象容器结构“解耦。

缺点

  • 使得设计更加复杂。客户端需要花更多时间理清类之间的层次关系。
  • 在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。

使用场景

  • 当想表达对象的部分-整体的层次结构时。(部分、整体场景,如树形菜单,文件、文件夹的管理。)
  • 希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象时。

示例

安全式组合模式

public interface Component {
    /**
     *  输出组建自身的名称
     */
    void operation(String name);
}
public class Composite implements Component {
    /**
     * 组合对象的名字
     */
    private String name;
    /**
     * 用来存储组合对象中包含的子组件对象
     */
    private List<Component> componentList = new ArrayList<>();

    /**
     * 传入组合对象的名称
     *
     * @param name 组合对象的名称
     */
    public Composite(String name) {
        this.name = name;
    }

    /**
     * 聚集管理方法,增加一个子构件对象
     *
     * @param component 子构件对象
     */
    public void addComponent(Component component) {
        this.addComponent(component);
    }

    /**
     * 聚集管理方法,返回所有子构件对象
     */
    public List<Component> getComponent() {
        return componentList;
    }
    /**
     * 输出对象的自身结构
     * @param preStr 前缀,主要是按照层级拼接空格,实现向后缩进
     */
    /**
     * 聚集管理方法,删除一个子构件对象
     *
     * @param index 子构件对象的下标
     */
    public void removeComponent(int index) {
        componentList.remove(index);
    }


    /**
     * 输出组建自身的名称
     */
    @Override
    public void operation(String name) {
        System.out.println(this.name);
        if (this.componentList != null) {
            for (Component c : componentList) {
                c.operation(name);
            }
        }
    }
}

public class Leaf implements Component {
    private String name;

    public Leaf(String name) {
        this.name = name;
    }

    @Override
    public void operation(String name) {
        System.out.println(name + "-" + this.name);
    }
}

public class Client {
    public static void main(String[] args) {
        Composite root = new Composite("我的电脑");
        Composite c1 = new Composite("C盘");
        Composite c2 = new Composite("D盘");

        Leaf leaf1 = new Leaf("文件1");
        Leaf leaf2 = new Leaf("文件2");
        Leaf leaf3 = new Leaf("文件3");
        Leaf leaf4 = new Leaf("文件4");

        root.addComponent(c1);
        root.addComponent(c2);
        c1.addComponent(leaf1);
        c1.addComponent(leaf2);
        c2.addComponent(leaf3);
        c2.addComponent(leaf4);

        root.operation("test");
    }
}

透明式组合模式

public abstract class Component {

    public abstract void operation(String name);

    public void addComponent(Component component) {
        throw new UnsupportedOperationException("叶子节点不支持此功能");
    }

    public List<Component> getComponent() {
        throw new UnsupportedOperationException("叶子节点不支持此功能");
    }

    public void removeComponent(int index) {
        throw new UnsupportedOperationException("叶子节点不支持此功能");
    }

}
public class Composite extends Component {
    /**
     * 组合对象的名字
     */
    private String name;
    /**
     * 用来存储组合对象中包含的子组件对象
     */
    private List<Component> componentList = new ArrayList<>();

    /**
     * 传入组合对象的名称
     *
     * @param name 组合对象的名称
     */
    public Composite(String name) {
        this.name = name;
    }

    /**
     * 聚集管理方法,增加一个子构件对象
     *
     * @param component 子构件对象
     */
    @Override
    public void addComponent(Component component) {
        this.addComponent(component);
    }

    /**
     * 聚集管理方法,返回所有子构件对象
     */
    @Override
    public List<Component> getComponent() {
        return componentList;
    }
    /**
     * 输出对象的自身结构
     * @param preStr 前缀,主要是按照层级拼接空格,实现向后缩进
     */
    /**
     * 聚集管理方法,删除一个子构件对象
     *
     * @param index 子构件对象的下标
     */
    @Override
    public void removeComponent(int index) {
        componentList.remove(index);
    }

    /**
     * 输出组建自身的名称
     */
    @Override
    public void operation(String name) {
        System.out.println(this.name);
        if (this.componentList != null) {
            for (Component c : componentList) {
                c.operation(name);
            }
        }
    }
}
public class Leaf extends Component {
    private String name;

    public Leaf(String name) {
        this.name = name;
    }

    @Override
    public void operation(String name) {
        System.out.println(name + "-" + this.name);
    }

}
public class Client {
    public static void main(String[] args) {
        Composite root = new Composite("我的电脑");
        Composite c1 = new Composite("C盘");
        Composite c2 = new Composite("D盘");

        Component leaf1 = new Leaf("文件1");
        Component leaf2 = new Leaf("文件2");
        Component leaf3 = new Leaf("文件3");
        Component leaf4 = new Leaf("文件4");

        root.addComponent(c1);
        root.addComponent(c2);
        c1.addComponent(leaf1);
        c1.addComponent(leaf2);
        c2.addComponent(leaf3);
        c2.addComponent(leaf4);

        root.operation("test");
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容