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

可能会增加系统的复杂性

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

相关推荐
小白黑_21612 分钟前
设计模式笔记
笔记·设计模式
liang89994 小时前
设计模式之装饰器模式(Decorator)
设计模式·装饰器模式
CocoaAndYy4 小时前
设计模式-适配器模式
设计模式·适配器模式
刷帅耍帅4 小时前
设计模式-适配器模式
设计模式·适配器模式
拥有一颗学徒的心6 小时前
设计模式——命令模式
设计模式·命令模式
拉里小猪的迷弟9 小时前
设计模式-结构型-常用:代理模式、桥接模式、装饰者模式、适配器模式
设计模式·代理模式·桥接模式·适配器模式·装饰器模式
CocoaAndYy11 小时前
设计模式-单例模式
单例模式·设计模式
bobostudio199513 小时前
TypeScript 设计模式之【策略模式】
前端·javascript·设计模式·typescript·策略模式
ok!ko17 小时前
设计模式之原型模式(通俗易懂--代码辅助理解【Java版】)
java·设计模式·原型模式
拉里小猪的迷弟18 小时前
设计模式-创建型-常用:单例模式、工厂模式、建造者模式
单例模式·设计模式·建造者模式·工厂模式