设计模式学习笔记之组合模式

什么是组合模式?

组合模式的定义

Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.
将对象组合成树形结构以表示 “部分-整体” 的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

主要优缺点

优点:
当需要在组合内添加新对象的时候,不需要修改源码,复合开闭原则。
对外提供的是同一个对象,使用起来不必关心使用的是单个对象还是组合对象。
缺点:
设计起来比较复杂,需要理清类之间的层次关系。

使用场景

系统对象之间存在整体–部分的关系,并有一定的相同行为。(JAVA中的swing,公司组织架构等等)

组合模式的结构和实现

组合模式结构

抽象根角色(Component):定义系统层级之间的相同行为,是树枝和叶子构建角色的公共抽象接口。
树枝角色(Composite):定义树枝角色的行为,可以存储子节点,子节点可以是树枝角色也可以是叶子角色,通过组合树枝角色和叶子角色形成一个树形结构。
叶子角色(Leaf):定义叶子角色的行为,其下再无分支。

主要有两种实现方式:透明性组合模式和安全性组合模式

透明性组合模式实现

在透明性组合模式当中,抽象根角色(Component)包含了树枝角色和叶子角色的所有行为。所以在叶子角色当中会存在树枝角色的行为,但是这些行为叶子角色是不支持的。就违背了接口隔离原则。

public class ComponentTest {
    public static void main(String[] args) {
        // 创建一个根节点
        Component root = new Composite("根节点");
        // 创建树枝节点
        Component branchA = new Composite("---树枝A");
        Component branchB = new Composite("------树枝B");
        // 来一个叶子节点
        Component leafA = new Leaf("------叶子A");
        Component leafB = new Leaf("------叶子B");
        Component leafC = new Leaf("---叶子C");

        root.addChild(branchA);
        root.addChild(leafC);
        branchA.addChild(leafA);
        branchA.addChild(branchB);
        branchB.addChild(leafB);

        String result = root.operation();
        System.out.println(result);
    }

    // 抽象构建角色
    static abstract class Component {
        String name;

        Component(String name) {
            this.name = name;
        }

        public abstract String operation();

        public boolean addChild(Component component) {
            throw new UnsupportedOperationException("addChild not supported!");
        }
        public Component getChild(int index) {
            throw new UnsupportedOperationException("getChild not supported!");
        }
    }

    //树枝构建角色
    static class Composite extends Component {
        private List<Component> childComponent;

        public Composite(String name) {
            super(name);
            this.childComponent = new ArrayList<>();
        }

        @Override
        public String operation() {
            StringBuilder builder = new StringBuilder(this.name);
            if(this.childComponent.size()>0){
                for (Component component : this.childComponent) {
                    builder.append("\n");
                    builder.append(component.operation());

                }
            }
            return builder.toString();
        }

        @Override
        public boolean addChild(Component component) {
            return this.childComponent.add(component);
        }

        @Override
        public Component getChild(int index) {
            return this.childComponent.get(index);
        }
    }

    //叶子构建角色
    static class Leaf extends Component {

        public Leaf(String name) {
            super(name);
        }

        @Override
        public String operation() {
            return this.name;
        }
    }

}

安全性组合模式实现

在安全性组合模式当中,抽象根角色(Component)包含了树枝角色和叶子角色的公共行为。在叶子角色当中就不再存在树枝角色的行为。

public class ComponentTest2 {
    public static void main(String[] args) {
        // 创建一个根节点
        Composite root = new Composite("根节点");
        // 创建树枝节点
        Composite branchA = new Composite("---树枝A");
        Composite branchB = new Composite("------树枝B");
        // 来一个叶子节点
        Component leafA = new Leaf("------叶子A");
        Component leafB = new Leaf("------叶子B");
        Component leafC = new Leaf("---叶子C");

        root.addChild(branchA);
        root.addChild(leafC);
        branchA.addChild(leafA);
        branchA.addChild(branchB);
        branchB.addChild(leafB);

        String result = root.operation();
        System.out.println(result);
    }

    // 抽象构建角色
    static abstract class Component {
        String name;

        Component(String name) {
            this.name = name;
        }

        public abstract String operation();

    }

    //树枝构建角色
    static class Composite extends Component {
        private List<Component> childComponent;

        public Composite(String name) {
            super(name);
            this.childComponent = new ArrayList<>();
        }

        @Override
        public String operation() {
            StringBuilder builder = new StringBuilder(this.name);
            if(this.childComponent.size()>0){
                for (Component component : this.childComponent) {
                    builder.append("\n");
                    builder.append(component.operation());

                }
            }
            return builder.toString();
        }


        public boolean addChild(Component component) {
            return this.childComponent.add(component);
        }


        public Component getChild(int index) {
            return this.childComponent.get(index);
        }
    }

    //叶子构建角色
    static class Leaf extends Component {

        public Leaf(String name) {
            super(name);
        }

        @Override
        public String operation() {
            return this.name;
        }
    }

}

发布了74 篇原创文章 · 获赞 24 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章