天天看点

设计模式-组合模式-Composite? 组合模式-Composite

? 组合模式-Composite

源码地址: https://github.com/mumushuiding/golang-design-pattern/tree/master/05_Composite

组合模式就像是一个? 树形结构,基本单位是? 叶 对象,基本节点互相聚集在一起就是? 组合 对象

❓ 问题

客户只需要知道节点的抽象的 

设计模式-组合模式-Composite? 组合模式-Composite

 接口,树形结构中的节点在工作时无论是单一还是组合的都会同样处理

✔️ 解决方法

组合模式的基础是定义一个抽象类 

设计模式-组合模式-Composite? 组合模式-Composite

Node,通过 接口 和契约确定它的行为.?叶类Leaf是Node的派生类,? 不能 聚集节点. 组合 对象Compoud是Node的派生类,可以? 聚集 多个Node节点.

⚡️类图

设计模式-组合模式-Composite? 组合模式-Composite

?参与者

组合模式的参与者如下:

  • 设计模式-组合模式-Composite? 组合模式-Composite
    Node (节点).抽象类Node确定了派生类的接口和行为.
  • ? Leaf (叶).体现了树形结构中的一个终结元素.
  • ? Compound (组合).它可以聚集其他节点.

?评价

?优点

优点:

  • 设计模式-组合模式-Composite? 组合模式-Composite
    叶和组合具有相同的接口,客户可以 统一 处理
  • 设计模式-组合模式-Composite? 组合模式-Composite
    可以多层 嵌套

?缺点

缺点:

  • 过多地使用不同的叶类和组合类,设计和构造树形结构就会条理不清.

继续阅读