设计模式之组合模式

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

组合模式有三个角色:

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

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

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);
    }
}
相关推荐
sg_knight4 小时前
Python 设计模式:迭代器模式——用优雅的方式遍历一切
python·设计模式·迭代器模式
ximu_polaris9 小时前
设计模式(C++)-行为型模式-解释器模式
c++·设计模式·解释器模式
钝挫力PROGRAMER10 小时前
static final 指向可变集合的设计模式
java·设计模式
霍格沃兹测试学院-小舟畅学12 小时前
我花一周拆解了企业级Skills库的全套设计模式
人工智能·设计模式
kyriewen1113 小时前
你的前端滤镜慢得像PPT?用Rust+WebAssembly,一秒处理4K图
开发语言·前端·javascript·设计模式·rust·ecmascript·powerpoint
混迹中的咸鱼13 小时前
C++ 23种设计模式
设计模式·c++23
咖啡八杯14 小时前
GoF设计模式——工厂方法模式
java·后端·设计模式
ximu_polaris1 天前
设计模式(C++)-行为型模式-中介者模式
c++·设计模式·中介者模式
诙_1 天前
深入理解C++设计模式
c++·设计模式
AI大法师1 天前
从门头到社媒预热图,快闪项目如何统一视觉输出
大数据·人工智能·设计模式