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

相关推荐
蜡笔小新..8 小时前
【设计模式】软件设计原则——开闭原则&里氏替换&单一职责
java·设计模式·开闭原则·单一职责原则
性感博主在线瞎搞9 小时前
【面向对象】设计模式概念和分类
设计模式·面向对象·中级软件设计师·设计方法
lucifer3119 小时前
JavaScript 中的组合模式(十)
javascript·设计模式
lucifer3119 小时前
JavaScript 中的装饰器模式(十一)
javascript·设计模式
蜡笔小新..10 小时前
【设计模式】软件设计原则——依赖倒置&合成复用
设计模式·依赖倒置原则·合成复用原则
刷帅耍帅10 小时前
设计模式-代理模式
设计模式·代理模式
神的孩子都在歌唱17 小时前
行为设计模式 -观察者模式- JAVA
java·观察者模式·设计模式
刷帅耍帅1 天前
设计模式-解释器模式
设计模式·解释器模式
刷帅耍帅1 天前
设计模式-备忘录模式
设计模式·备忘录模式