天天看点

2022设计模式之组合模式(Composite Pattern),又叫部分整体模式

作者:王東東Rex

复合设计模式将对象组合成树结构,以表示部分-整体层次结构。此模式允许客户端统一处理单个对象和对象的组合。

介绍

意图:将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

主要解决:它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

何时使用: 1、您想表示对象的部分-整体层次结构(树形结构)。 2、您希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

如何解决:树枝和叶子实现统一接口,树枝内部组合该接口。

关键代码:树枝内部组合该接口,并且含有内部属性 List,里面放 Component。

应用实例: 1、算术表达式包括操作数、操作符和另一个操作数,其中,另一个操作数也可以是操作数、操作符和另一个操作数。 2、在 JAVA AWT 和 SWING 中,对于 Button 和 Checkbox 是树叶,Container 是树枝。

优点: 1、高层模块调用简单。 2、节点自由增加。

缺点:在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。

使用场景:部分、整体场景,如树形菜单,文件、文件夹的管理。

注意事项:定义时为具体类。

UML类图

2022设计模式之组合模式(Composite Pattern),又叫部分整体模式

C# 中的结构代码

此结构代码演示了复合模式,该模式允许创建树结构,在树结构中,无论各个节点是叶节点还是分支(复合)节点,都可以统一访问这些节点。

  • using System;
  • using System.Collections.Generic;
  • namespace Composite.Structural
  • {
  • /// <summary>
  • /// Composite Design Pattern
  • /// </summary>
  • public class Program
  • public static void Main(string[] args)
  • // Create a tree structure
  • Composite root = new Composite("root");
  • root.Add(new Leaf("Leaf A"));
  • root.Add(new Leaf("Leaf B"));
  • Composite comp = new Composite("Composite X");
  • comp.Add(new Leaf("Leaf XA"));
  • comp.Add(new Leaf("Leaf XB"));
  • root.Add(comp);
  • root.Add(new Leaf("Leaf C"));
  • // Add and remove a leaf
  • Leaf leaf = new Leaf("Leaf D");
  • root.Add(leaf);
  • root.Remove(leaf);
  • // Recursively display tree
  • root.Display(1);
  • // Wait for user
  • Console.ReadKey();
  • }
  • /// The 'Component' abstract class
  • public abstract class Component
  • protected string name;
  • // Constructor
  • public Component(string name)
  • this.name = name;
  • public abstract void Add(Component c);
  • public abstract void Remove(Component c);
  • public abstract void Display(int depth);
  • /// The 'Composite' class
  • public class Composite : Component
  • List<Component> children = new List<Component>();
  • public Composite(string name)
  • : base(name)
  • public override void Add(Component component)
  • children.Add(component);
  • public override void Remove(Component component)
  • children.Remove(component);
  • public override void Display(int depth)
  • Console.WriteLine(new String('-', depth) + name);
  • // Recursively display child nodes
  • foreach (Component component in children)
  • component.Display(depth + 2);
  • /// The 'Leaf' class
  • public class Leaf : Component
  • public Leaf(string name)
  • public override void Add(Component c)
  • Console.WriteLine("Cannot add to a leaf");
  • public override void Remove(Component c)
  • Console.WriteLine("Cannot remove from a leaf");
2022设计模式之组合模式(Composite Pattern),又叫部分整体模式

C# 中的实际代码

此实际代码演示了用于构建由基元节点(线条、圆圈等)和复合节点(组成更复杂元素的绘图元素组)组成的图形树结构时使用的复合模式。

  • namespace Composite.RealWorld
  • CompositeElement root = new CompositeElement("Picture");
  • root.Add(new PrimitiveElement("Red Line"));
  • root.Add(new PrimitiveElement("Blue Circle"));
  • root.Add(new PrimitiveElement("Green Box"));
  • // Create a branch
  • CompositeElement comp = new CompositeElement("Two Circles");
  • comp.Add(new PrimitiveElement("Black Circle"));
  • comp.Add(new PrimitiveElement("White Circle"));
  • // Add and remove a PrimitiveElement
  • PrimitiveElement pe = new PrimitiveElement("Yellow Line");
  • root.Add(pe);
  • root.Remove(pe);
  • // Recursively display nodes
  • /// The 'Component' Treenode
  • public abstract class DrawingElement
  • public DrawingElement(string name)
  • public abstract void Add(DrawingElement d);
  • public abstract void Remove(DrawingElement d);
  • public abstract void Display(int indent);
  • public class PrimitiveElement : DrawingElement
  • public PrimitiveElement(string name)
  • public override void Add(DrawingElement c)
  • Console.WriteLine(
  • "Cannot add to a PrimitiveElement");
  • public override void Remove(DrawingElement c)
  • "Cannot remove from a PrimitiveElement");
  • public override void Display(int indent)
  • new String('-', indent) + " " + name);
  • public class CompositeElement : DrawingElement
  • List<DrawingElement> elements = new List<DrawingElement>();
  • public CompositeElement(string name)
  • public override void Add(DrawingElement d)
  • elements.Add(d);
  • public override void Remove(DrawingElement d)
  • elements.Remove(d);
  • Console.WriteLine(new String('-', indent) +
  • "+ " + name);
  • // Display each child element on this node
  • foreach (DrawingElement d in elements)
  • d.Display(indent + 2);
2022设计模式之组合模式(Composite Pattern),又叫部分整体模式

继续阅读