• 设计模式day11


    组合模式

    组合模式也称为整体-部分模式,它的宗旨是通过将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口进行表示,使得客户对单个对象和组合对象的使用具有一致性,属于结构型模式。

    角色

    1抽象根节点:定义系统各层对象的共有方法和属性,可以预先定义一些默认行为和属性;
    2,树枝节点:定义树枝节点的行为,存储子节点,组合树枝节点和叶子节点形成一个树形结构:
    3,叶子节点:叶子节点对象,其下再无分支,是系统层次遍历的最小单位
    组合模式再代码具体实现有两种方式,分别是透明组合模式和安全组合模式。

    场景

    当子系统与其内各个对象层呈现树形结构时,可以使用组合模式让子系统内各个对象层次的行为操作具备一致性。客户端使用该子系统内任何一个层次对象时,无须进行区分,直接使用通用操作即可。
    1,希望客户端可以忽略组合对象与单个对象的差异时;
    2,对象层次具备整体和部分,呈树形结构。

    透明组合模式的写法

    public abstract class CourseComponent {
    
        public void addChild(CourseComponent catalogComponent){
            throw new UnsupportedOperationException("不支持添加操作");
        }
    
        public void removeChild(CourseComponent catalogComponent){
            throw new UnsupportedOperationException("不支持删除操作");
        }
    
    
        public String getName(CourseComponent catalogComponent){
            throw new UnsupportedOperationException("不支持获取名称操作");
        }
    
    
        public double getPrice(CourseComponent catalogComponent){
            throw new UnsupportedOperationException("不支持获取价格操作");
        }
    
    
        public void print(){
            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
    public class Course extends CourseComponent {
        private String name;
        private double price;
    
        public Course(String name, double price) {
            this.name = name;
            this.price = price;
        }
    
        @Override
        public String getName(CourseComponent catalogComponent) {
            return this.name;
        }
    
        @Override
        public double getPrice(CourseComponent catalogComponent) {
            return this.price;
        }
    
        @Override
        public void print() {
            System.out.println(name + " (¥" + price + "元)");
        }
    
    }
    
    
    • 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
    public class CoursePackage extends CourseComponent {
        private List<CourseComponent> items = new ArrayList<CourseComponent>();
        private String name;
        private Integer level;
    
    
        public CoursePackage(String name, Integer level) {
            this.name = name;
            this.level = level;
        }
    
        @Override
        public void addChild(CourseComponent catalogComponent) {
            items.add(catalogComponent);
        }
    
        @Override
        public String getName(CourseComponent catalogComponent) {
            return this.name;
        }
    
        @Override
        public void removeChild(CourseComponent catalogComponent) {
            items.remove(catalogComponent);
        }
    
        @Override
        public void print() {
            System.out.println(this.name);
    
            for(CourseComponent catalogComponent : items){
                //控制显示格式
                if(this.level != null){
                    for(int  i = 0; i < this.level; i ++){
                        //打印空格控制格式
                        System.out.print("  ");
                    }
                    for(int  i = 0; i < this.level; i ++){
                        //每一行开始打印一个+号
                        if(i == 0){ System.out.print("+"); }
                        System.out.print("-");
                    }
                }
                //打印标题
                catalogComponent.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
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    public class Test {
        public static void main(String[] args) {
    
            System.out.println("============透明组合模式===========");
    
            CourseComponent javaBase = new Course("Java入门课程",8280);
            CourseComponent ai = new Course("人工智能",5000);
    
            CourseComponent packageCourse = new CoursePackage("Java架构师课程",2);
    
            CourseComponent design = new Course("Java设计模式",1500);
            CourseComponent source = new Course("源码分析",2000);
            CourseComponent softSkill = new Course("软技能",3000);
    
            packageCourse.addChild(design);
            packageCourse.addChild(source);
            packageCourse.addChild(softSkill);
    
            CourseComponent catalog = new CoursePackage("课程主目录",1);
            catalog.addChild(javaBase);
            catalog.addChild(ai);
            catalog.addChild(packageCourse);
    
            catalog.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
    • 25
    • 26
    • 27
    • 28
    • 29

    好处客户端无需分辨时叶子节点和树枝节点,他们具备完全一致的接口
    缺点是叶子节点会继承得到一些它所不需要的方法,这与设计模式接口隔离原则相违背。

    安全组合模式的写法

    安全组合模式是只规定系统各个层次的最基础的一致行为,而把组合本身的方法放在自身当中。

    
    public abstract class Directory {
    
        protected String name;
    
        public Directory(String name) {
            this.name = name;
        }
    
        public abstract void show();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    
    public class File extends Directory {
    
        public File(String name) {
            super(name);
        }
    
        @Override
        public void show() {
            System.out.println(this.name);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Folder extends Directory {
        private List<Directory> dirs;
    
        private Integer level;
    
        public Folder(String name,Integer level) {
            super(name);
            this.level = level;
            this.dirs = new ArrayList<Directory>();
        }
    
        @Override
        public void show() {
            System.out.println(this.name);
            for (Directory dir : this.dirs) {
                //控制显示格式
                if(this.level != null){
                    for(int  i = 0; i < this.level; i ++){
                        //打印空格控制格式
                        System.out.print("  ");
                    }
                    for(int  i = 0; i < this.level; i ++){
                        //每一行开始打印一个+号
                        if(i == 0){ System.out.print("+"); }
                        System.out.print("-");
                    }
                }
                //打印名称
                dir.show();
            }
        }
    
        public boolean add(Directory dir) {
            return this.dirs.add(dir);
        }
    
        public boolean remove(Directory dir) {
            return this.dirs.remove(dir);
        }
    
        public Directory get(int index) {
            return this.dirs.get(index);
        }
    
        public void list(){
            for (Directory dir : this.dirs) {
                System.out.println(dir.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
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    
    
    
    class Test {
        public static void main(String[] args) {
    
            System.out.println("============安全组合模式===========");
    
            File qq = new File("QQ.exe");
            File wx = new File("微信.exe");
    
            Folder office = new Folder("办公软件",2);
    
            File word = new File("Word.exe");
            File ppt = new File("PowerPoint.exe");
            File excel = new File("Excel.exe");
    
            office.add(word);
            office.add(ppt);
            office.add(excel);
    
            Folder wps = new Folder("金山软件",3);
            wps.add(new File("WPS.exe"));
            office.add(wps);
    
            Folder root = new Folder("根目录",1);
            root.add(qq);
            root.add(wx);
            root.add(office);
    
            System.out.println("----------show()方法效果-----------");
            root.show();
    
            System.out.println("----------list()方法效果-----------");
            root.list();
    
    
        }
    }
    
    • 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

    安全组合模式的好处是接口定义职责清晰,符合设计模式,单一职责原则和接口隔离原理
    缺点是客户需要区分树枝节点和叶子节点,这样才能正确处理各个层次的操作,客户端无法依赖抽象,违背了设计模式依赖倒置原则。

    组合模式优缺点

    很多小伙伴肯定还有一个疑问,既然组合模式分为两种实现,什么时候用透明组合模式,什么时候用安全组合模式,透明组合模式将公共接口封装到抽象根节点(Component)中,那么系统所以节点就具备一致行为,所以如果当系统绝大多数层次具备相同的公共行为时,采用透明组合模式也许会更好;而如果当系统各个层次差异性行为较多或树枝节点层相对稳定时,采用安全组合模式。
    优点:
    1清楚地定义分层次的复杂对象,表示对象的全部或部分层次
    2,让客户端忽略了层次的差异,方便对整个层次结构进行控制
    3简化客户端代码
    4符合开闭原则
    缺点
    1,限制类型时会较为复杂
    2,使设计变得更加抽象

  • 相关阅读:
    linux 合并两个文件夹中的方法
    软考高级(信息系统项目管理师)高频考点-项目管理计划
    讯飞星火认知大模型Java后端接口
    文本四字节unicode解析出错
    《影响力》笔记
    一文搞懂深度信念网络!DBN概念介绍与Pytorch实战
    Linux Commands Interview questions
    Spring Boot 2.6.x整合Swagger启动失败报错问题解决(治标还治本)
    功能测试:核心原理、挑战以及解决之道
    Ubuntu安装clickhouse集群
  • 原文地址:https://blog.csdn.net/weixin_49349744/article/details/125378023