前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式学习笔记之组合模式

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

作者头像
海加尔金鹰
发布2020-06-09 10:15:55
3510
发布2020-06-09 10:15:55
举报

什么是组合模式?

组合模式的定义

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)包含了树枝角色和叶子角色的所有行为。所以在叶子角色当中会存在树枝角色的行为,但是这些行为叶子角色是不支持的。就违背了接口隔离原则。

代码语言:javascript
复制
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)包含了树枝角色和叶子角色的公共行为。在叶子角色当中就不再存在树枝角色的行为。

代码语言:javascript
复制
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;
        }
    }

}

标题:设计模式学习笔记之组合模式 作者:海加尔金鹰 地址:https://www.hjljy.cn/articles/2019/12/24/1577116855811.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是组合模式?
    • 组合模式的定义
      • 主要优缺点
        • 使用场景
        • 组合模式的结构和实现
          • 组合模式结构
            • 透明性组合模式实现
              • 安全性组合模式实现
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档