设计模式之组合模式

概念:将对象组合成树形结构以表示"部分------整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

组合模式有三个角色:

  • 抽象构件:定义公有属性和方法。
  • 叶子结点:树形结构的底层结点,没有子结点,实现抽象构件的所有操作。
  • 中间结点:叶子结点之前的结点,有子结点。

组合模式最经典的例子就是文件和文件夹结构。下面用一个这样的例子来帮助大家理解组合模式。

java 复制代码
public abstract class FileComponent {
    protected String name;
    public FileComponent(String name) {
        this.name = name;
    }
    public abstract void add(FileComponent component);
    public abstract void remove(FileComponent component);
    public abstract void display(int depth);
    public String getName() {
        return name;
    }
}

public class FileNode extends FileComponent {
    public FileNode(String name) {
        super(name);
    }
    @Override
    public void add(FileComponent component) {
        throw new UnsupportedOperationException("Cannot add components to a file.");
    }
    @Override
    public void remove(FileComponent component) {
        throw new UnsupportedOperationException("Cannot remove components from a file.");
    }
    @Override
    public void display(int depth) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append("--");
        }
        System.out.println(sb + name);
    }
}

public class Directory extends FileComponent {
    private List<FileComponent> children;
    public Directory(String name) {
        super(name);
        children = new ArrayList<>();
    }
    @Override
    public void add(FileComponent component) {
        children.add(component);
    }
    @Override
    public void remove(FileComponent component) {
        children.remove(component);
    }
    @Override
    public void display(int depth) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append("--");
        }
        System.out.println(sb + name);
        for (FileComponent child : children) {
            child.display(depth + 1);
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        Directory root = new Directory("Root");
        Directory documents = new Directory("Documents");
        Directory pictures = new Directory("Pictures");
        FileNode readme = new FileNode("Readme.txt");
        FileNode image = new FileNode("image.jpg");
        root.add(documents);
        root.add(pictures);
        documents.add(readme);
        pictures.add(image);
        System.out.println("File structure:");
        root.display(0);
    }
}
相关推荐
researcher-Jiang6 小时前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
饼干哥哥14 小时前
用Claude Code跑通跨境电商的5大场景,顶10个人的团队
人工智能·设计模式·正则表达式
折哥的程序人生 · 物流技术专研15 小时前
第3篇:手写促销策略引擎(满减、打折、立减)
java·设计模式·策略模式·开闭原则·编程实战·扩充系列·促销引擎
折哥的程序人生 · 物流技术专研17 小时前
第3篇:手写一个饮品制作模板(附代码)
java·设计模式·行为型模式·模版方法模式·钩子方法·代码复用·编程实战
ttod_qzstudio18 小时前
【软考设计模式】单例模式:唯一实例的管控与线程安全精讲
单例模式·设计模式
折哥的程序人生 · 物流技术专研19 小时前
第4篇:模板方法 vs 策略模式,面试怎么选?
java·设计模式·策略模式·行为型模式·模版方法模式·扩充系列·继承v组合
小园子的小菜19 小时前
Java设计模式精讲:创建型+结构型模式(含生产级案例+相似模式对比)
学习·设计模式
geovindu2 天前
java:Abstract Factory Pattern
java·开发语言·后端·设计模式·抽象工厂模式·创建型模式
geovindu2 天前
java: Factory Method Pattern
java·开发语言·后端·设计模式·工厂方法模式·创建型模式
折哥的程序人生 · 物流技术专研2 天前
第2篇:模板方法模式核心:好莱坞原则
设计模式·uml·模板方法模式·行为型模式·钩子方法·扩充系列·好莱坞原则