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

可能会增加系统的复杂性

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

相关推荐
是糖糖啊12 小时前
Agent 不好用?先别怪模型,试试 Harness Engineering
人工智能·设计模式
jiankeljx12 小时前
Spring Boot 经典九设计模式全览
java·spring boot·设计模式
WarrenMondeville13 小时前
5.Unity面向对象-依赖倒置原则
unity·设计模式·依赖倒置原则
加个鸡腿儿1 天前
从"包裹器"到"确认按钮"——一个组件的三次重构
前端·vue.js·设计模式
ALex_zry1 天前
现代C++设计模式实战:从AIDC项目看工业级代码架构
c++·设计模式·架构
君主黑暗1 天前
设计模式-工厂模式
设计模式
han_1 天前
JavaScript设计模式(四):发布-订阅模式实现与应用
前端·javascript·设计模式
bmseven1 天前
23种设计模式 - 工厂方法(Factory Method)
设计模式·工厂方法模式
pqq的迷弟1 天前
设计模式的原则
设计模式
君主黑暗1 天前
设计模式-抽象工厂模式
设计模式·抽象工厂模式