小谈设计模式(20)—组合模式

小谈设计模式(20)---组合模式

专栏介绍

专栏地址

link

专栏介绍

主要对目前市面上常见的23种设计模式进行逐一分析和总结,希望有兴趣的小伙伴们可以看一下,会持续更新的。希望各位可以监督我,我们一起学习进步,加油,各位。

组合模式

组合模式是一种结构型设计模式,它允许将对象组合成树状结构以表示"部分-整体"的层次结构。组合模式使得客户端可以统一地处理单个对象和组合对象,无需区分它们的区别。

对象类型

叶节点(Leaf)和组合节点(Composite)

叶节点

它表示树的最底层的对象,它们没有子节点。

组合节点

它表示树的分支节点,它可以包含其他的组合节点和叶节点。

核心思想

使用一个抽象类或接口来定义组合节点和叶节点的公共操作。这样,客户端可以通过调用这些公共操作来处理组合节点和叶节点,而无需知道具体的节点类型。

应用场景

1

需要表示对象的部分-整体层次结构,并且希望客户端能够一致地处理单个对象和组合对象的情况。

2

需要对树状结构进行递归操作,例如遍历树、查找特定节点等。

3

需要动态地增加或删除树的节点。

结构图

结构图分析

在上面的结构图中,Component 是组合模式的抽象类或接口,定义了组合节点和叶节点共有的操作。Composite 是组合节点的具体实现,它可以包含其他的组合节点和叶节点。Leaf 是叶节点的具体实现。

Java语言实现

首先,我们需要定义一个抽象的组件类 Component,它包含了组合节点和叶节点的公共操作:

java 复制代码
public abstract class Component {
    protected String name;

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

    public abstract void operation();

    public abstract void add(Component component);

    public abstract void remove(Component component);

    public abstract Component getChild(int index);
}

然后,我们定义组合节点类 Composite,它实现了 Component 接口,并包含了一个子组件列表:

java 复制代码
import java.util.ArrayList;
import java.util.List;

public class Composite extends Component {
    private List<Component> children;

    public Composite(String name) {
        super(name);
        children = new ArrayList<>();
    }

    @Override
    public void operation() {
        System.out.println("Composite " + name + " operation.");
        for (Component component : children) {
            component.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);
    }
}

最后,我们定义叶节点类 Leaf,它也实现了 Component 接口,但它没有子节点:

java 复制代码
public class Leaf extends Component {
    public Leaf(String name) {
        super(name);
    }

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

    @Override
    public void add(Component component) {
        // 叶节点不支持添加操作
    }

    @Override
    public void remove(Component component) {
        // 叶节点不支持删除操作
    }

    @Override
    public Component getChild(int index) {
        // 叶节点没有子节点
        return null;
    }
}

现在,我们可以使用组合模式来创建一个树状结构并操作它:

java 复制代码
public class Main {
    public static void main(String[] args) {
        // 创建树状结构
        Composite root = new Composite("root");
        Composite branch1 = new Composite("branch1");
        Composite branch2 = new Composite("branch2");
        Leaf leaf1 = new Leaf("leaf1");
        Leaf leaf2 = new Leaf("leaf2");
        Leaf leaf3 = new Leaf("leaf3");

        root.add(branch1);
        root.add(branch2);
        branch1.add(leaf1);
        branch2.add(leaf2);
        branch2.add(leaf3);

        // 调用操作方法
        root.operation();
    }
}

运行上述代码,输出结果如下

复制代码
Composite root operation.
Composite branch1 operation.
Leaf leaf1 operation.
Composite branch2 operation.
Leaf leaf2 operation.
Leaf leaf3 operation.

总结

以上就是使用Java语言实现组合模式的示例代码。通过组合模式,我们可以方便地处理树状结构,并且客户端可以一致地处理单个对象和组合对象。

优缺点分析

优点

简化客户端代码

客户端可以一致地处理单个对象和组合对象,无需区分它们的差异。

增加新的节点类型

通过继承 Component 类,可以方便地增加新的节点类型,而无需修改现有的代码。

方便地处理递归结构

组合模式适用于处理递归结构,例如树状结构。

缺点

可能会导致设计过于一般化

组合模式将叶节点和组合节点都抽象为 Component 类,可能会导致设计过于一般化,不适合特定的场景。

可能会增加系统的复杂性

组合模式引入了组合节点和叶节点的层次结构,可能会增加系统的复杂性。

相关推荐
这是谁的博客?5 小时前
微服务架构设计模式深度解析:从拆分策略到容灾机制
微服务·设计模式·云原生·架构·架构设计·后端开发·分布式系统
fan_music9 小时前
设计模式学习
c++·设计模式
乐观的山里娃11 小时前
【后编码时代 06】Vibe Coding + Superpowers 完全不够
设计模式·软件工程·ai编程
likerhood12 小时前
设计模式 · 责任链模式(Chain of Responsibility Pattern)
设计模式·责任链模式
追烽少年x14 小时前
STL中的设计模式(一)
c++·设计模式
乐观的山里娃15 小时前
【设计模式 10】抽象工厂:整体换季
设计模式
贵慜_Derek17 小时前
《从零实现 Agent 系统》连载 08|编排与工作流:从 Chat 到任务图
人工智能·设计模式·架构
W.W.H.17 小时前
C++ 设计模式:6 个常用模式的实战示例
开发语言·c++·设计模式
老码观察17 小时前
设计模式实战解读(三):模板方法模式——骨架复用与扩展点设计
设计模式·模板方法模式
雪度娃娃18 小时前
行为型设计模式——状态模式
ui·设计模式·状态模式