A Sort Of A Blog
旧游无处不堪寻,无寻处,惟有少年心
设计模式-组合

组合模式(Composite Pattern)是一种结构型设计模式,你可以使用它将对象组合成树状结构,并且能像使用独立对象一样使用它们。

解释


如果应用的核心模型能用树状结构表示,在应用中使用组合模式才有价值

组合最主要的功能是在整个树状结构上递归调用方法并对结果进行汇总。

组合模式 UML


适用场景


  1. 如果你需要实现树状对象结构,可以使用组合模式
  2. 如果你希望客户端代码以相同方式处理简单和复杂元素,可以使用该模式

代码示例


using System;
namespace CompositePatternDemo
{
abstract class Component
{
public Component() { }

public abstract string Operation();

public virtual void Add(Component component)
{
throw new NotImplementedException();
}

public virtual void Remove(Component component)
{
throw new NotImplementedException();
}

public virtual bool IsComposite()
{
return true;
}
}

class Leaf : Component
{
public override string Operation()
{
return "Leaf";
}

public override bool IsComposite()
{
return false;
}
}

class Composite : Component
{
protected List<Component> _children = new List<Component>();

public override void Add(Component component)
{
this._children.Add(component);
}

public override void Remove(Component component)
{
this._children.Remove(component);
}

public override string Operation()
{
int i = 0;
string result = "Branch(";

foreach (Component component in this._children)
{
result += component.Operation();
if (i != this._children.Count - 1)
{
result += "+";
}
i++;
}

return result + ")";
}
}

class Client
{
public void ClientCode(Component leaf)
{
Console.WriteLine($"RESULT: {leaf.Operation()}\n");
}

public void ClientCode2(Component component1, Component component2)
{
if (component1.IsComposite())
{
component1.Add(component2);
}

Console.WriteLine($"RESULT: {component1.Operation()}");
}
}

class Program
{
static void Main(string[] args)
{
Client client = new Client();

Leaf leaf = new Leaf();
Console.WriteLine("Client: I get a simple component:");
client.ClientCode(leaf);

Composite tree = new Composite();
Composite branch1 = new Composite();
branch1.Add(new Leaf());
branch1.Add(new Leaf());
Composite branch2 = new Composite();
branch2.Add(new Leaf());
tree.Add(branch1);
tree.Add(branch2);
Console.WriteLine("Client: Now I've got a composite tree:");
client.ClientCode(tree);

Console.Write("Client: I don't need to check the components classes even when managing the tree:\n");
client.ClientCode2(tree, leaf);
// output

// Client: I get a simple component:
// RESULT: Leaf

// Client: Now I've got a composite tree:
// RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf))

// Client: I don't need to check the components classes even when managing the tree:
// RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf)+Leaf)
}
}
}