小谈设计模式(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 类,可能会导致设计过于一般化,不适合特定的场景。

可能会增加系统的复杂性

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

相关推荐
ApeAssistant1 小时前
Spring + 设计模式 (二十) 行为型 - 中介者模式
spring·设计模式
ApeAssistant1 小时前
Spring + 设计模式 (十九) 行为型 - 访问者模式
spring·设计模式
〆、风神1 小时前
从零实现分布式WebSocket组件:设计模式深度实践指南
分布式·websocket·设计模式
前端大白话2 小时前
Vue2和Vue3语法糖差异大揭秘:一文读懂,开发不纠结!
javascript·vue.js·设计模式
前端大白话2 小时前
JavaScript中`Symbol.for()`和`Symbol()`的区别,在创建全局唯一的`Symbol`值时如何选择使用?
前端·javascript·设计模式
CHQIUU2 小时前
Java 设计模式心法之第25篇 - 中介者 (Mediator) - 用“中央协调”降低对象间耦合度
java·设计模式·中介者模式
Pasregret4 小时前
备忘录模式:实现对象状态撤销与恢复的设计模式
运维·服务器·设计模式
碎梦归途5 小时前
23种设计模式-行为型模式之备忘录模式(Java版本)
java·jvm·设计模式·软考·备忘录模式·软件设计师·行为型模式
东阳马生架构13 小时前
Sentinel源码—8.限流算法和设计模式总结二
算法·设计模式·sentinel
冰茶_15 小时前
C#中常见的设计模式
java·开发语言·microsoft·设计模式·微软·c#·命令模式