当前位置:   article > 正文

设计模式- 组合模式(Composite)结构|原理|优缺点|场景|示例_组合设计模式

组合设计模式

                                     设计模式(分类)        设计模式(六大原则)   

    创建型(5种)        工厂方法         抽象工厂模式        单例模式        建造者模式        原型模式

    结构型(7种)        适配器模式        装饰器模式        代理模式        ​​​​​​外观模式      桥接模式        组合模式       享元模式

    行为型(11种)      策略模式        模板方法模式        观察者模式        迭代器模式     责任链模式     命令模式    备忘录模式          状态模式         访问者模式        中介者模式 


组合模式(Composite Pattern)是一种结构型设计模式,它允许你将对象组合成树形结构来表示“部分-整体”的层次结构。这种模式使得客户端可以以一致的方式处理单个对象(叶子节点)和组合对象(容器节点),无需关心处理的是个体还是群体。组合模式使得你可以将对象看作是树形结构中的节点,节点可以是简单的对象,也可以是包含其他节点的复合对象,这样就能形成一个层次化的结构。

模式结构

组合模式主要涉及以下几个角色:

  1. Component(抽象组件)

    • 定义了所有对象(包括叶子节点和容器节点)共享的公共接口。这个接口规定了如何访问和管理对象的子部件。
    • 通常会提供一个方法来添加、删除子组件,以及遍历子组件的方法。
  2. Leaf(叶子组件)

    • 是组合结构的终端节点,不包含任何子组件。
    • 实现Component接口,但对于那些与子组件管理无关的方法(如添加、删除子组件),可以提供空实现或者抛出异常。
  3. Composite(容器组件)

    • 包含一个或多个子组件,每个子组件也是Component的实例。
    • 实现Component接口,提供与管理子组件相关的方法的实际逻辑,如添加、删除子组件以及遍历子组件。
    • 可能会提供一些额外的方法来管理子组件集合,但这些方法通常不暴露给客户端。

工作原理

  • 客户端:通过Component接口与系统交互,无需区分处理的是叶子节点还是容器节点。
  • Component:定义了通用接口,为所有组件(包括叶子和容器)提供一致性。
  • Leaf:实现Component接口,但不包含子组件,因此与子组件管理相关的操作为空或无效。
  • Composite:除了实现Component接口外,还持有子组件的集合,并提供操作子组件的方法。当客户端请求操作时,Composite会递归地将请求传递给它的子组件。

优缺点

优点
  • 单一职责原则:组合模式使得叶子节点和容器节点都遵循单一职责原则,各自专注于自己的功能。
  • 透明性:客户端可以一致地处理单个对象和组合对象,无需知道处理的是叶子还是容器,提高了代码的透明性和简洁性。
  • 易于扩展:新类型的组件只需继承Component或实现相关接口即可加入到组合结构中,不影响已有代码。
缺点
  • 设计复杂度增加:为了实现组合模式,需要设计额外的抽象层和接口,使得系统变得相对复杂。
  • 递归操作可能导致性能问题:如果组合结构非常深,递归操作可能会导致栈溢出或效率下降。

适用场景

  • 系统需要处理对象的“部分-整体”关系:当需要表示对象的层级结构时,组合模式可以很好地表示这种关系。
  • 希望客户端以一致的方式处理单个对象和组合对象:组合模式使得客户端无需关心处理对象的具体类型,简化了客户端代码。
  • 希望简化新组件类型的添加:新的叶子节点或容器节点只需要符合Component接口即可轻松融入现有系统。

代码示例(以Java为例)

  1. // 抽象组件
  2. interface Component {
  3. void add(Component component);
  4. void remove(Component component);
  5. void operation();
  6. }
  7. // 叶子节点
  8. class Leaf implements Component {
  9. private String name;
  10. public Leaf(String name) {
  11. this.name = name;
  12. }
  13. @Override
  14. public void add(Component component) {
  15. throw new UnsupportedOperationException("Leaves cannot have children.");
  16. }
  17. @Override
  18. public void remove(Component component) {
  19. throw new UnsupportedOperationException("Leaves cannot have children.");
  20. }
  21. @Override
  22. public void operation() {
  23. System.out.println("Leaf " + name + " performing operation.");
  24. }
  25. }
  26. // 容器节点
  27. class Composite implements Component {
  28. private List<Component> children = new ArrayList<>();
  29. private String name;
  30. public Composite(String name) {
  31. this.name = name;
  32. }
  33. @Override
  34. public void add(Component component) {
  35. children.add(component);
  36. }
  37. @Override
  38. public void remove(Component component) {
  39. children.remove(component);
  40. }
  41. @Override
  42. public void operation() {
  43. System.out.println("Composite " + name + " performing operation.");
  44. for (Component child : children) {
  45. child.operation();
  46. }
  47. }
  48. }
  49. // 客户端代码
  50. public class CompositePatternDemo {
  51. public static void main(String[] args) {
  52. Component root = new Composite("Root");
  53. root.add(new Leaf("Leaf A"));
  54. root.add(new Leaf("Leaf B"));
  55. Component branch = new Composite("Branch");
  56. branch.add(new Leaf("Leaf C"));
  57. branch.add(new Leaf("Leaf D"));
  58. root.add(branch);
  59. root.operation();
  60. }
  61. }

在这个Java示例中:

  • Component接口定义了所有组件(叶子和容器)的通用接口,包括添加、删除子组件和执行操作的方法。
  • Leaf类实现了Component接口,但其addremove方法抛出异常,表示叶子节点无法添加或删除子节点。operation方法输出叶子节点执行操作的信息。
  • Composite类同样实现了Component接口,并维护了一个List<Component>来存储子组件。addremove方法实现了对子组件的增删操作。operation方法不仅执行自身操作,还递归地调用其子组件的operation方法。
  • 客户端代码创建了一个树状结构,并通过调用根节点的operation方法,以一致的方式处理整个组合结构中的所有组件。

 代码示例(以Python为例)

  1. from abc import ABC, abstractmethod
  2. class FileSystemObject(ABC):
  3. """抽象组件(Component)"""
  4. def __init__(self, name):
  5. self.name = name
  6. @abstractmethod
  7. def add(self, child):
  8. pass
  9. @abstractmethod
  10. def remove(self, child):
  11. pass
  12. @abstractmethod
  13. def get_child(self, index):
  14. pass
  15. @abstractmethod
  16. def operation(self):
  17. pass
  18. class File(FileSystemObject):
  19. """叶子组件(Leaf)"""
  20. def add(self, child):
  21. raise TypeError("Cannot add children to a file")
  22. def remove(self, child):
  23. raise TypeError("Cannot remove children from a file")
  24. def get_child(self, index):
  25. raise IndexError("Files do not have children")
  26. def operation(self):
  27. return f"Performing operation on file: {self.name}"
  28. class Directory(FileSystemObject):
  29. """复合组件(Composite)"""
  30. def __init__(self, name):
  31. super().__init__(name)
  32. self.children = []
  33. def add(self, child):
  34. self.children.append(child)
  35. def remove(self, child):
  36. self.children.remove(child)
  37. def get_child(self, index):
  38. return self.children[index]
  39. def operation(self):
  40. result = f"Performing operation on directory: {self.name}\n"
  41. for child in self.children:
  42. result += child.operation() + "\n"
  43. return result
  44. # 客户端代码
  45. if __name__ == "__main__":
  46. root_dir = Directory("root")
  47. dir_a = Directory("dir_a")
  48. dir_b = Directory("dir_b")
  49. file_1 = File("file_1.txt")
  50. file_2 = File("file_2.txt")
  51. root_dir.add(dir_a)
  52. root_dir.add(dir_b)
  53. dir_a.add(file_1)
  54. dir_b.add(file_2)
  55. print(root_dir.operation())

 在这个Python示例中:

  • FileSystemObject是抽象组件,使用abc模块中的ABC类和abstractmethod装饰器定义了所有文件系统对象共有的接口,如添加、删除子对象和执行操作等。
  • File类作为叶子组件,继承自FileSystemObject,实现了operation方法,并且其addremoveget_child方法抛出异常,表示文件不能包含子对象。
  • Directory类作为复合组件,同样继承自FileSystemObject,并且维护了一个children列表来存储子对象(可以是文件或子目录)。addremoveget_child方法实现了对子对象的管理。operation方法不仅执行自身操作,还递归地调用其子对象的operation方法,从而处理整个目录树。

客户端代码创建了根目录、子目录以及文件,并建立了它们之间的层级关系。最后,通过调用根目录的operation方法,以一致的方式处理整个文件系统的对象,无论是单个文件还是包含多个子对象的目录。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/1022402
推荐阅读
相关标签
  

闽ICP备14008679号