• 设计模式-Composite模式(组合模式)


    什么是Composite组合模式

    能够使容器与内容具有一致性,创造出递归结构的模式就是Composite模式。

    以文件系统为例子,文件夹(director)既是一种存放文件(file)的容器,而文件夹本身也可以作为一种文件,存放在容器内。所以有了抽象文件(Entry),表示一个类既可以便是普通文件,也可以表示容器,这样就能做到容器内又可以存普通文件,又能存放容器,只要它们都是“抽象文件”即可。

    代码实现

    抽象文件Entry,有一个默认方法add,表示如果是容器就可以继续增加抽象文件

    public abstract class Entry {
    
        public abstract String getName();
        public abstract int getSize();
        public Entry add(Entry entry) throws FileTreatmentException {
            throw new FileTreatmentException();
        }
    
        public void printList() {
            printList("");
        }
    
        protected abstract void printList(String prefix);
    
        public String toString() {
            return getName() + " (" + getSize() + ")";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    自定义异常,表示非容器类型不能调用add方法

    public class FileTreatmentException extends RuntimeException {
        public FileTreatmentException() {
        }
    
        public FileTreatmentException(String message) {
            super(message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    普通文件File,只作为内容

    public class File extends Entry{
    
        private String name;
        private int size;
    
        public File(String name, int size) {
            this.name = name;
            this.size = size;
        }
    
        @Override
        public String getName() {
            return name;
        }
    
        @Override
        public int getSize() {
            return size;
        }
    
        @Override
        protected void printList(String prefix) {
            System.out.println(prefix + "/" + this);
        }
    }
    
    • 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

    文件夹Director,既可以作为内容,又可以作为容器

    public class Directory extends Entry {
        private String name;
        private ArrayList directory = new ArrayList();
    
        public Directory(String name) {
            this.name = name;
        }
    
        @Override
        public String getName() {
            return name;
        }
    
        @Override
        public int getSize() {
            int size = 0;
            Iterator it = directory.iterator();
            while (it.hasNext()) {
                Entry entry = (Entry) it.next();
                size += entry.getSize();
            }
            return size;
        }
    
        @Override
        public Entry add(Entry entry) throws FileTreatmentException {
            directory.add(entry);
            return this;
        }
    
        @Override
        protected void printList(String prefix) {
            System.out.println(prefix + "/" + this);
            Iterator it = directory.iterator();
            while (it.hasNext()) {
                Entry entry = (Entry) it.next();
                entry.printList(prefix + "/" + 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

    测试类

    public class Main {
        public static void main(String[] args) {
            System.out.println("Making root entries...");
            Directory rootdir = new Directory("root");
    
            Directory bindir = new Directory("bindir");
            Directory tmpdir = new Directory("tmpdir");
            Directory usr = new Directory("usr");
    
            rootdir.add(bindir);
            rootdir.add(tmpdir);
            rootdir.add(usr);
    
            bindir.add(new File("vi", 10000));
            bindir.add(new File("latex", 20000));
            rootdir.printList();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    为什么要用Composite模式

    使内容和容器具有一致性,可以创建出递归结构

    在这里插入图片描述

  • 相关阅读:
    python 中指定GPU乱序解决方案
    什么是开源智能家居操作系统?用智汀家庭云搭建一套自己的智能家居
    Hall定理(霍尔定理)证明及推广
    表面和光栅区域的通道配置
    车道线检测2022新工作整理,2D、3D都有
    人工智能 | ShowMeAI资讯日报 #2022.06.22
    Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?
    S5的未来:即将到来的协议改进和可能性
    如何利用软文推广进行SEO优化(打造优质软文,提升网站排名)
    大名鼎鼎的OceanBase居然在买Star了?
  • 原文地址:https://blog.csdn.net/weixin_44030848/article/details/126212333