0
点赞
收藏
分享

微信扫一扫

C#设计模式之:组合模式


组合模式Composite

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

UML

C#设计模式之:组合模式_子类

代码

abstract class Component
{
protected string name;

public Component(string name)
{
this.name = name;
}

public abstract void Add(Component c);
public abstract void Remove(Component c);
public abstract void Display(int

class Composite : Component
{
private List<Component> children = new List<Component>();

public Composite(string name): base(name)
{

}

public override void Add(Component c)
{
children.Add(c);
}

public override void Remove(Component c)
{
children.Remove(c);
}

public override void Display(int depth)
{
Console.WriteLine(new string('-', depth) + name);

foreach (Component component in children)
{
component.Display(depth + 2);
}
}
}

class Leaf : Component
{
public Leaf(string name) : base(name)
{

}

public override void Add(Component c)
{
Console.WriteLine("Cannot add a leaf");
}

public override void Remove(Component c)
{
Console.WriteLine("Cannot remove a leaf");
}

public override void Display(int depth)
{
Console.WriteLine(new string('-', depth) + name);
}
}

// test
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);

Composite comp2 = new Composite("Composite XY");
comp2.Add(new Leaf("Leaf XYA"));
comp2.Add(new Leaf("Leaf XYB"));
root.Add(comp2);

root.Add(new Leaf("Leaf C"));
Leaf leaf = new Leaf("Leaf D");
root.Add(leaf);
root.Remove(leaf);

root.Display(1);

// result
-root
---Leaf A---Leaf B---Composite X-----Leaf XA-----Leaf XB---Composite XY-----Leaf XYA-----Leaf XYB---Leaf C

本质

整体和部分可以被一致对待

透明方式

Leaf叶类中也有Add 与 Remove方法,这种方式叫透明方式,也就是说在Component中声明所有用来管理子对象的方法,其中包括Add、Remove等。这样实现Component接口的所有子类都具备了Add与Remove。这样做的好处是叶节点和枝节点对于外界没有区别,它们具有一致的行为接口。但问题也很明显,因为Leaf类本身不具备Add、Remove方法的功能,其实现是没有意义的

安全方式

在Component接口中不去声明Add与Remove方法,那么子类Leaf也就不用必须实现它们,而在Composite类中声明所有用来管理子类对象的方法,但不够透明,所以Leaf 与 Composite类将不具有相同的接口,客户端调用需要做相应的判断,带来了不便。

何时使用组合模式?

当发现需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑使用组合模式了

案例

UI的一系列控件就是使用了组合模式


举报

相关推荐

0 条评论