当前位置: 首页 > news >正文

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

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

  • 专栏介绍
    • 专栏地址
    • 专栏介绍
  • 组合模式
    • 对象类型
      • 叶节点
      • 组合节点
    • 核心思想
    • 应用场景
      • 1
      • 2
      • 3
    • 结构图
      • 结构图分析
    • Java语言实现
      • 首先,我们需要定义一个抽象的组件类 Component,它包含了组合节点和叶节点的公共操作:
      • 然后,我们定义组合节点类 Composite,它实现了 Component 接口,并包含了一个子组件列表:
      • 最后,我们定义叶节点类 Leaf,它也实现了 Component 接口,但它没有子节点:
      • 现在,我们可以使用组合模式来创建一个树状结构并操作它:
      • 运行上述代码,输出结果如下
      • 总结
    • 优缺点分析
      • 优点
        • 简化客户端代码
        • 增加新的节点类型
        • 方便地处理递归结构
      • 缺点
        • 可能会导致设计过于一般化
        • 可能会增加系统的复杂性

专栏介绍

专栏地址

link

专栏介绍

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

组合模式

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

对象类型

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

叶节点

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

组合节点

它表示树的分支节点,它可以包含其他的组合节点和叶节点。
在这里插入图片描述

核心思想

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

应用场景

1

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

2

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

3

需要动态地增加或删除树的节点。
在这里插入图片描述

结构图

在这里插入图片描述

结构图分析

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

Java语言实现

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

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 接口,并包含了一个子组件列表:

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<>();}@Overridepublic void operation() {System.out.println("Composite " + name + " operation.");for (Component component : children) {component.operation();}}@Overridepublic void add(Component component) {children.add(component);}@Overridepublic void remove(Component component) {children.remove(component);}@Overridepublic Component getChild(int index) {return children.get(index);}
}

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

public class Leaf extends Component {public Leaf(String name) {super(name);}@Overridepublic void operation() {System.out.println("Leaf " + name + " operation.");}@Overridepublic void add(Component component) {// 叶节点不支持添加操作}@Overridepublic void remove(Component component) {// 叶节点不支持删除操作}@Overridepublic Component getChild(int index) {// 叶节点没有子节点return null;}
}

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

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

可能会增加系统的复杂性

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

http://www.lryc.cn/news/183340.html

相关文章:

  • sheng的学习笔记-【中文】【吴恩达课后测验】Course 1 - 神经网络和深度学习 - 第三周测验
  • 一文详解动态链表和静态链表的区别
  • [C国演义] 第十三章
  • <二>Qt斗地主游戏开发:过场动画的实现
  • 链式法则(Chain Rule)
  • AUTOSAR COM模块框架梳理
  • 详细介绍区块链之挖矿
  • 华为OD机试真题-路灯照明问题(Java/C++/Go/Python)
  • 嵌入式技术面试基本规则
  • osg实现自定义插件读取自定义格式的模型文件到场景
  • redis进阶
  • (一)正点原子STM32MP135移植——准备
  • Kotlin的关键字 lateinit 和 lazy
  • 阿里云服务器ECS详细介绍_云主机_服务器托管_弹性计算
  • 12、建立健全人员培训体系
  • 代码随想录算法训练营第五十九天 | 647. 回文子串 516.最长回文子序列
  • React Redux
  • StreamingLLM - 处理无限长度的输入
  • [Linux 命令] nm 详解
  • 好文学作品的鉴赏标准
  • 智慧公厕:将科技融入日常生活的创新之举
  • ROS(0)命令及学习资源汇总
  • NodeMCU ESP8266开发流程详解(图文并茂)
  • 【最终版】tkinter+matplotlib实现一个强大的绘图系统
  • Postman使用实例
  • 【ES的优势和原理及分布式开发的好处与坏处】
  • Autosar诊断实战系列23-CanTp半/全双工及相关工程问题思考
  • 【Pandas】数据分组groupby
  • 【图像处理GIU】图像分割(Matlab代码实现)
  • Java中的锁与锁优化技术