设计模式之组合模式

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

组合模式有三个角色:

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

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

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);
    }
}
相关推荐
cooldream200912 小时前
深入理解MVP架构:让UI层与业务逻辑完美分离的设计模式
ui·设计模式·架构·系统架构师
摘星编程14 小时前
并发设计模式实战系列(3):工作队列
设计模式·并发编程
Pasregret15 小时前
访问者模式:分离数据结构与操作的设计模式
数据结构·设计模式·访问者模式
Aniugel17 小时前
JavaScript高级面试题
javascript·设计模式·面试
不当菜虚困18 小时前
JAVA设计模式——(四)门面模式
java·开发语言·设计模式
Niuguangshuo18 小时前
Python设计模式:MVC模式
python·设计模式·mvc
Lei活在当下18 小时前
【现代 Android APP 架构】01. APP 架构综述
android·设计模式·架构
前端大白话18 小时前
震惊!90%前端工程师都踩过的坑!computed属性vs methods到底该怎么选?一文揭秘高效开发密码
前端·vue.js·设计模式
前端大白话18 小时前
前端必看!figure标签在响应式图片排版中的王炸操作,grid/flex布局实战指南
前端·设计模式·html
ApeAssistant19 小时前
Spring + 设计模式 (十四) 行为型 - 观察者模式
spring·设计模式