• 12【组合设计模式】



    十二、组合设计模式

    12.1 组合设计模式简介

    12.1.1 组合设计模式概述

    组合设计模式(Composite Pattern):又名部分整体模式,它把对象组合成树形结构以表示“部分-整体”的层次结构,并且将单个对象(叶子节点)与组合对象(树枝节点)使用相同的接口表示,使得用户对单个对象和组合对象的使用具有一致性。

    组合模式一般用来描述整体与部分之间的关系,他将对象组织到树形结构中,顶层的节点为根节点,根节点下面可以包含树枝节点和叶子节点,树枝节点下面又可以包含树枝节点和叶子节点;叶子节点则是普通节点,下面不可以包含其他节点;

    • 如图所示:

    在这里插入图片描述

    在组合模式之中,会把树枝节点和叶子节点看作属于同一种数据类型(用同一父类定义),让他们具备同样的行为,这样一来在组合模式中整个树形结构中的对象都属于同一种类型;这样的好处就是用户不需要分辨该节点是树枝节点还是叶子节点,都可以直接进行操作;

    组合模式的主要使用场景就是维护和展示部分-整体关系的场景,如树形菜单、文件和文件夹、部门层级管理等。

    12.1.2 组合设计模式中的角色

    组合模式中包含有如下角色:

    • 抽象根节点(Component):定义系统各层次对象的共有方法和属性,可以预先定义一些默认行为和属性。
    • 树枝节点(Composite):定义树枝节点的行为,存储子节点,组合树枝节点和叶子节点形成一个树形结构。
    • 叶子节点(Leaf):叶子节点对象,其下再无分支,是系统层次遍历的最小单位。

    12.2 组合设计模式的实现

    12.2.1 透明组合模式

    透明组合模式就是把所有的公共方法都定义在Component中,这样做的好处是,客户端无需分辨叶子节点和树枝节点,因为他们完全具备同样的方法;

    【文件夹与文件案例】

    一个文件夹中可能包含多个文件夹或文件,使用组合设计模式来描述这种关系的存在;

    UML类图如下:

    在这里插入图片描述

    • 抽象根节点:
    package com.pattern.demo01_透明组合模式;
    
    /**
     * @author lscl
     * @version 1.0
     * @intro:
     */
    public abstract class AbstractComponent {
    
        // 该枝干的名称
        protected String name;
        // 层级
        protected Integer level;
    
        public void add(AbstractComponent component) {
            throw new UnsupportedOperationException("不支持添加操作!");
        }
    
        public void remove(AbstractComponent component) {
            throw new UnsupportedOperationException("不支持移除操作!");
        }
    
        public void print() {
            throw new UnsupportedOperationException("不支打印操作!");
        }
    
        public String getName() {
            throw new UnsupportedOperationException("不支持获取名字操作!");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 树枝节点(整体):
    package com.pattern.demo01_透明组合模式;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author lscl
     * @version 1.0
     * @intro: 树枝节点(整体)
     */
    public class FilesComponent extends AbstractComponent {
    
        // 下属的节点(有可能是树枝节点,也有可能是叶子节点)
        private List<AbstractComponent> componentList = new ArrayList<>();
    
        public FilesComponent(String name, Integer level) {
            this.name = name;
            this.level = level;
        }
    
        @Override
        public void add(AbstractComponent component) {
            componentList.add(component);
        }
    
        @Override
        public void remove(AbstractComponent component) {
            componentList.remove(component);
        }
    
        @Override
        public void print() {
            for (int i = 1; i < level; i++) {
                System.out.print("  ");
            }
            System.out.println(name);
            for (AbstractComponent component : componentList) {
                component.print();
            }
    
        }
    
        @Override
        public String getName() {
            return this.name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 叶子节点(部分):
    package com.pattern.demo01_透明组合模式;
    
    /**
     * @author lscl
     * @version 1.0
     * @intro: 叶子节点(部分)
     */
    public class FileComponent extends AbstractComponent {
    
        public FileComponent(String name, Integer level) {
            this.name = name;
            this.level = level;
        }
    
        @Override
        public void print() {
            for (int i = 1; i <level; i++) {
                System.out.print("  ");
            }
            System.out.println(name);
        }
    
        @Override
        public String getName() {
            return name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 测试代码:
    package com.pattern.demo01_透明组合模式;
    
    /**
     * @author lscl
     * @version 1.0
     * @intro:
     */
    public class Demo01 {
        public static void main(String[] args) {
            AbstractComponent aaa = new FilesComponent("aaa", 1);
    
            aaa.add(new FileComponent("000.txt",2));
            aaa.add(new FileComponent("001.txt",2));
    
            FilesComponent bbb = new FilesComponent("bbb", 2);
            bbb.add(new FileComponent("002.txt",3));
            bbb.add(new FileComponent("003.txt",3));
    
            aaa.add(bbb);
            
            aaa.print();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    运行效果:

    在这里插入图片描述

    • 小结:

    透明组合模式中把所有功能方法都定义在Component中,这样做的好处是确保所有的构件类都有相同的接口,对于客户端来说就不需要区分对象是叶子节点还是树枝节点,透明组合模式也是组合模式的标准形式。

    但是透明组合模式的缺点还是不够安全,因为叶子对象和容器对象在本质上是有区别的,叶子对象不可能有下一个层次的对象,即不可能包含成员对象,因此为其提供 add()、remove() 等方法是没有意义的,这在编译阶段不会出错,但在运行阶段如果调用这些方法可能会出错。另外这样的做法让叶子节点存在很多不相关的方法,违反了接口隔离原则;

    12.2.2 安全组合模式

    在安全组合模式中,在抽象构件角色中没有声明任何用于管理成员对象的方法,而是在Composite/Leaf中声明并实现这些方法。安全组合模式的缺点是不够透明,因为Leaf和Component具有不同的方法,且容器构件中那些用于管理成员对象的方法没有在抽象构件类中定义,因此客户端不能完全针对抽象编程,必须有区别地对待Leaf和Component。违背了依赖倒置原则;

    • 抽象根节点:
    package com.pattern.demo02_安全组合模式;
    
    /**
     * @author lscl
     * @version 1.0
     * @intro: 抽象根节点
     */
    public abstract class AbstractComponent {
        protected String name;
        protected Integer level;
    
        public abstract void print();
    
        public abstract String getName();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 树枝节点(整体):
    package com.pattern.demo02_安全组合模式;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author lscl
     * @version 1.0
     * @intro: 树枝节点(整体部分)
     */
    public class FilesComponent extends AbstractComponent {
        private List<AbstractComponent> componentList = new ArrayList<>();
    
        public FilesComponent(String name, Integer level) {
            this.name = name;
            this.level = level;
        }
    
        // 树枝节点独特的方法
        public void add(AbstractComponent component){
            componentList.add(component);
        }
    
        // 树枝节点独特的方法
        public void remove(AbstractComponent component){
            componentList.remove(component);
        }
    
        @Override
        public void print() {
            for (int i = 1; i < level; i++) {
                System.out.println("  ");
            }
            System.out.println(name);
    
            for (AbstractComponent component : componentList) {
                component.print();
            }
        }
    
        @Override
        public String getName() {
            return name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 叶子节点(部分)
    package com.pattern.demo02_安全组合模式;
    
    
    /**
     * @author lscl
     * @version 1.0
     * @intro: 叶子节点(部分)
     */
    public class FileComponent extends AbstractComponent {
    
        public FileComponent(String name, Integer level) {
            this.name = name;
            this.level = level;
        }
    
        @Override
        public void print() {
            for (int i = 1; i <level; i++) {
                System.out.print("  ");
            }
            System.out.println(name);
        }
    
        @Override
        public String getName() {
            return name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 测试类:
    package com.pattern.demo02_安全组合模式;
    
    /**
     * @author lscl
     * @version 1.0
     * @intro:
     */
    public class Demo01 {
        public static void main(String[] args) {
    
            // 安全组合模式不能使用Component组件来接收,因为已经将功能定义到了树枝节点中
            FilesComponent aaa = new FilesComponent("aaa", 1);
    
            aaa.add(new FileComponent("000.txt",2));
            aaa.add(new FileComponent("001.txt",2));
    
            FilesComponent bbb = new FilesComponent("bbb", 2);
            bbb.add(new FileComponent("002.txt",3));
            bbb.add(new FileComponent("003.txt",3));
    
            aaa.add(bbb);
            aaa.print();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    12.3 组合设计模式的优缺点

    • 什么时候使用透明/安全组合模式?

      • 透明组合模式将公共接口封装到抽象根节点中,那么系统所有节点都具备一致性行为,因此如果系统绝大多数层次都具备相同的公共行为,则采用透明组合模式会更好,其代价是为剩下的少数层次节点引入不需要的方法;
      • 安全组合模式则是将具体的行为延迟到了树枝/叶子节点来实现,这样做的好处是其他层级节点不会引入额外的方法来引发一系列的安全问题,其代价是客户端必须有区别的对待Leaf和Component组件,不符合依赖倒置原则;
    • 组合模式的优点:

      • 透明/安全组合模式都可以清楚的描述"部分-整体"之间的层次关系;
      • 透明组合模式对客户端友好,不需要区分叶子节点和树枝节点,符合依赖倒置原则,扩展性强
      • 安全组合模式叶子节点和树枝节点功能更加明确化,符合接口隔离原则;
    • 组合模式的缺点:

      • 透明组合模式:
        • 1)叶子节点继承的安全问题
        • 2)违反接口隔离原则
      • 安全组合模式:
        • 1)需要客户端来区分叶子节点和树枝节点,不符合依赖倒置原则
  • 相关阅读:
    计算机毕业设计Java足球青训俱乐部管理后台系统(源码+系统+mysql数据库+Lw文档)
    Paparazzi: Surface Editing by way of Multi-View Image Processing
    O2OA 7.2.0新版本直播预告!快来了解一下吧!
    【通信原理】通信系统概念、组成、分类、度量的分析与研究
    1 基础知识
    GDB调试ROS功能包
    vue-cli解决IE浏览器chunk-vendors.js 提示sockjs-client错误
    java基于springboot的高校图书馆座位预约
    Javascript正则解析出代码的函数体
    大数据数仓建模基础理论【维度表、事实表、数仓分层及示例】
  • 原文地址:https://blog.csdn.net/Bb15070047748/article/details/126368638