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

相关推荐
静水流深_沧海一粟15 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder15 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室1 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo5 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4965 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃5 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
驴儿响叮当20105 天前
设计模式之状态模式
设计模式·状态模式
电子科技圈5 天前
XMOS推动智能音频等媒体处理技术从嵌入式系统转向全新边缘计算
人工智能·mcu·物联网·设计模式·音视频·边缘计算·iot
徐先生 @_@|||5 天前
安装依赖三方exe/msi的软件设计模式
设计模式