设计模式——组合模式(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方法对整个结构进行操作。

相关推荐
AI人工智能+电脑小能手13 小时前
大白话说Java设计模式-23-桥接模式(源码剖析篇)
java·设计模式·jdbc·桥接模式·源码分析·awt·java logging
嘟嘟071716 小时前
TypeScript 工具类型通关:Pick、Omit、Partial、Record 区别与 Omit 等价实现一次讲清
设计模式·面试·typescript
SL_staff17 小时前
中小离散制造如何用工艺路线模板解决‘人走流程丢’——JVS-APS 实践解析
java·设计模式·全栈
Asize17 小时前
3 种设计模式 + DI:Nest.js 后端第一课
后端·设计模式·nestjs
St_rive1 天前
Page Object设计模式
java·开发语言·设计模式
烬羽2 天前
NestJS 依赖注入:Controller 里那个没有 new 的 service,到底从哪来的?
设计模式·typescript·nestjs
用户938515635072 天前
工厂模式与 Nest.js 核心思想 —— 从蜜雪冰城到企业级架构
后端·设计模式·nestjs
烬羽2 天前
从 nest new 到 Hello World:一个最小 NestJS 项目,讲透工厂 + 装饰器 + 模块化
设计模式·node.js·nestjs
艺艺生辉3 天前
从if-else到策略模式
后端·设计模式
小林ixn3 天前
单例模式:从弹窗管理到全局状态,一个模式搞定
前端·javascript·设计模式