组合模式

2019-08-04  本文已影响0人  闽越布衣

描述

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

简介

安全式组合模式
透明式组合模式

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

角色

优缺点

优点

缺点

使用场景

示例

安全式组合模式

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");
    }
}
上一篇 下一篇

猜你喜欢

热点阅读