设计模式——组合模式(Composite)

组合模式(Composite Pattern) 是一种结构型设计模式,它将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

特点

  1. 表示整体与部分:组合模式允许你将对象组合成树形结构来表示"部分-整体"的层次结构。这使得用户可以一致地对待单个对象和组合对象。

  2. 递归结构:组合模式采用递归的方式来定义对象的结构,使得每个对象都可以具有子对象,而子对象本身又可以是一个组合对象。

  3. 客户端透明性:客户端代码与单个对象和组合对象进行交互时,无需区分它们之间的差别。客户端代码可以统一地调用组合结构中的所有对象。

角色

  1. Component(抽象构件):定义了所有对象(包括叶子对象和容器对象)的公共接口,以及用于访问及管理子对象的接口。

  2. Leaf(叶子构件):实现了Component接口,是组合中的叶子节点对象,叶子节点没有子节点。

  3. Composite(容器构件):实现了Component接口,是容纳叶子节点和容器节点的容器对象,可以递归地包含其他容器对象和叶子对象。

适用场景

  1. 当你想表示对象的部分-整体的层次结构时。
  2. 当你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象时。
  3. 当你需要遍历组合结构中的对象,而无需关心对象的具体类型时(即用户无需关心到底是处理一个叶子节点还是处理一个容器节点)。

示例

以下是一个简单的组合模式示例,展示了文件夹和文件的层次结构:

java 复制代码
// 抽象构件
interface Component {
    void operation();
    void add(Component component);
    void remove(Component component);
    Component getChild(int index);
}

// 叶子构件
class Leaf implements Component {
    private String name;

    public Leaf(String name) {
        this.name = name;
    }

    @Override
    public void operation() {
        System.out.println("File " + name + " is operated.");
    }

    // 叶子节点没有子节点,所以以下方法为空实现
    @Override
    public void add(Component component) {
        throw new UnsupportedOperationException("Cannot add to a leaf");
    }

    @Override
    public void remove(Component component) {
        throw new UnsupportedOperationException("Cannot remove from a leaf");
    }

    @Override
    public Component getChild(int index) {
        throw new UnsupportedOperationException("Cannot get child from a leaf");
    }
}

// 容器构件
class Composite implements Component {
    private List<Component> children = new ArrayList<>();

    @Override
    public void operation() {
        for (Component child : children) {
            child.operation();
        }
    }

    @Override
    public void add(Component component) {
        children.add(component);
    }

    @Override
    public void remove(Component component) {
        children.remove(component);
    }

    @Override
    public Component getChild(int index) {
        return children.get(index);
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        Component root = new Composite();

        Component folder1 = new Composite();
        Component folder2 = new Composite();

        Component file1 = new Leaf("file1.txt");
        Component file2 = new Leaf("file2.txt");

        folder1.add(file1);
        folder2.add(file2);

        root.add(folder1);
        root.add(folder2);

        root.operation(); // 操作文件夹及其下的文件
    }
}

在上面的示例中,我们定义了Component接口作为抽象构件,Leaf类作为叶子构件,Composite类作为容器构件。客户端代码创建了一个包含文件夹和文件的组合结构,并调用operation方法对整个结构进行操作。

相关推荐
BD_Marathon1 天前
设计模式——合成复用原则
设计模式·合成复用原则
书院门前细致的苹果1 天前
设计模式大全:单例、工厂模式、策略模式、责任链模式
设计模式·责任链模式·策略模式
BD_Marathon2 天前
设计模式——依赖倒转原则
java·开发语言·设计模式
BD_Marathon2 天前
设计模式——里氏替换原则
java·设计模式·里氏替换原则
jmxwzy2 天前
设计模式总结
设计模式
J_liaty2 天前
23种设计模式一代理模式
设计模式·代理模式
苏渡苇3 天前
优雅应对异常,从“try-catch堆砌”到“设计驱动”
java·后端·设计模式·学习方法·责任链模式
短剑重铸之日3 天前
《设计模式》第十一篇:总结
java·后端·设计模式·总结
feasibility.3 天前
AI 编程助手进阶指南:从 Claude Code 到 OpenCode 的工程化经验总结
人工智能·经验分享·设计模式·自动化·agi·skills·opencode
BD_Marathon3 天前
七大设计原则介绍
设计模式