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

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

特点

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

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

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

角色

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

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

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

适用场景

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

示例

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

// 抽象构件
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方法对整个结构进行操作。

相关推荐

  1. 设计模式——组合模式Composite

    2024-05-09 13:14:07       38 阅读
  2. 设计模式--组合模式Composite Pattern)

    2024-05-09 13:14:07       51 阅读
  3. 设计模式组合模式Composite Pattern)

    2024-05-09 13:14:07       36 阅读
  4. 设计模式】10、composite 组合模式

    2024-05-09 13:14:07       35 阅读
  5. 设计模式-组合模式Composite(结构型)

    2024-05-09 13:14:07       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-09 13:14:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-09 13:14:07       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-09 13:14:07       82 阅读
  4. Python语言-面向对象

    2024-05-09 13:14:07       91 阅读

热门阅读

  1. Leetcode 199:二叉树的右视图

    2024-05-09 13:14:07       30 阅读
  2. Vue 组件参数传递:多个参数 vs 单个对象

    2024-05-09 13:14:07       34 阅读
  3. vue后端api开发

    2024-05-09 13:14:07       34 阅读
  4. CGAL在ubuntu下的安装及Hello World的测试

    2024-05-09 13:14:07       28 阅读
  5. 1700.无法吃午餐的学生数量

    2024-05-09 13:14:07       35 阅读
  6. Springboot-Jedis实现分布式锁

    2024-05-09 13:14:07       39 阅读
  7. shell_结束进程脚本

    2024-05-09 13:14:07       34 阅读
  8. deepspeed+transformers模型微调

    2024-05-09 13:14:07       29 阅读