组合模式(Composite Pattern)
——.NET设计模式系列之十一
Terrylee,2006年3月
概述
组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。
意图
将对象组合成树形结构以表示“部分-整体”的层次结构。Composite模式使得用户对单个对象和组合对象的使用具有一致性。[GOF 《设计模式》]
结构图
![](https://images.cnblogs.com/cnblogs_com/terrylee/composite.gif)
图1 Composite模式结构图
生活中的例子
组合模式将对象组合成树形结构以表示"部分-整体"的层次结构。让用户一致地使用单个对象和组合对象。虽然例子抽象一些,但是算术表达式确实是组合的例子。算术表达式包括操作数、操作符和另一个操作数。操作数可以是数字,也可以是另一个表达式。这样,2+3和(2+3)+(4*6)都是合法的表达式。
![](https://images.cnblogs.com/cnblogs_com/terrylee/Compiste01.jpg)
图2 使用算术表达式例子的Composite模式对象图
组合模式解说
这里我们用绘图这个例子来说明Composite模式,通过一些基本图像元素(直线、圆等)以及一些复合图像元素(由基本图像元素组合而成)构建复杂的图形树。在设计中我们对每一个对象都配备一个Draw()方法,在调用时,会显示相关的图形。可以看到,这里复合图像元素它在充当对象的同时,又是那些基本图像元素的一个容器。先看一下基本的类结构图:
![](https://images.cnblogs.com/cnblogs_com/terrylee/Compiste_04.jpg)
图3
图中橙色的区域表示的是复合图像元素。示意性代码:
public abstract class Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected string _name;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Graphics(string name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._name = name;
}
public abstract void Draw();
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Picture : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Picture(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ArrayList GetChilds()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//![](https://www.cnblogs.com/Images/dot.gif)
返回所有的子对象
}
}
而其他作为树枝构件,实现代码如下:
public class Line:Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Line(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Circle : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Circle(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Rectangle : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Rectangle(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
}
现在我们要对该图像元素进行处理:在客户端程序中,需要判断返回对象的具体类型到底是基本图像元素,还是复合图像元素。如果是复合图像元素,我们将要用递归去处理,然而这种处理的结果却增加了客户端程序与复杂图像元素内部结构之间的依赖,那么我们如何去解耦这种关系呢?我们希望的是客户程序可以像处理基本图像元素一样来处理复合图像元素,这就要引入Composite模式了,需要把对于子对象的管理工作交给复合图像元素,为了进行子对象的管理,它必须提供必要的Add(),Remove()等方法,类结构图如下:
![](https://images.cnblogs.com/cnblogs_com/terrylee/Compiste_05.jpg)
图4
示意性代码:
public abstract class Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected string _name;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Graphics(string name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._name = name;
}
public abstract void Draw();
public abstract void Add();
public abstract void Remove();
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Picture : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected ArrayList picList = new ArrayList();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Picture(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Graphics g in picList)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
g.Draw();
}
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Add(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
picList.Add(g);
}
public override void Remove(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
picList.Remove(g);
}
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Line : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Line(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
public override void Add(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
public override void Remove(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Circle : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Circle(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
public override void Add(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
public override void Remove(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Rectangle : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Rectangle(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
public override void Add(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
public override void Remove(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
}
这样引入Composite模式后,客户端程序不再依赖于复合图像元素的内部实现了。然而,我们程序中仍然存在着问题,因为Line,Rectangle,Circle已经没有了子对象,它是一个基本图像元素,因此Add(),Remove()的方法对于它来说没有任何意义,而且把这种错误不会在编译的时候报错,把错误放在了运行期,我们希望能够捕获到这类错误,并加以处理,稍微改进一下我们的程序:
public class Line : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Line(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
public override void Add(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//抛出一个我们自定义的异常
}
public override void Remove(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//抛出一个我们自定义的异常
}
}
这样改进以后,我们可以捕获可能出现的错误,做进一步的处理。上面的这种实现方法属于透明式的Composite模式,如果我们想要更安全的一种做法,就需要把管理子对象的方法声明在树枝构件Picture类里面,这样如果叶子节点Line,Rectangle,Circle使用这些方法时,在编译期就会出错,看一下类结构图:
![](https://images.cnblogs.com/cnblogs_com/terrylee/Compiste_06.jpg)
图5
示意性代码:
public abstract class Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected string _name;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Graphics(string name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._name = name;
}
public abstract void Draw();
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Picture : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected ArrayList picList = new ArrayList();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Picture(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Graphics g in picList)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
g.Draw();
}
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Add(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
picList.Add(g);
}
public void Remove(Graphics g)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
picList.Remove(g);
}
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Line : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Line(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Circle : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Circle(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Rectangle : Graphics
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Rectangle(string name)
: base(name)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Draw()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Draw a" + _name.ToString());
}
}
这种方式属于安全式的Composite模式,在这种方式下,虽然避免了前面所讨论的错误,但是它也使得叶子节点和树枝构件具有不一样的接口。这种方式和透明式的Composite各有优劣,具体使用哪一个,需要根据问题的实际情况而定。通过Composite模式,客户程序在调用Draw()的时候不用再去判断复杂图像元素中的子对象到底是基本图像元素,还是复杂图像元素,看一下简单的客户端调用:
public class App
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public static void Main()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Picture root = new Picture("Root");
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
root.Add(new Line("Line"));
root.Add(new Circle("Circle"));
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Rectangle r = new Rectangle("Rectangle");
root.Add(r);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
root.Draw();
}
}
.NET中的组合模式
如果有人用过Enterprise Library2.0,一定在源程序中看到了一个叫做ObjectBuilder的程序集,顾名思义,它是用来负责对象的创建工作的,而在ObjectBuilder中,有一个被称为定位器的东西,通过定位器,可以很容易的找到对象,它的结构采用链表结构,每一个节点是一个键值对,用来标识对象的唯一性,使得对象不会被重复创建。定位器的链表结构采用可枚举的接口类来实现,这样我们可以通过一个迭代器来遍历这个链表。同时多个定位器也被串成一个链表。具体地说就是多个定位器组成一个链表,表中的每一个节点是一个定位器,定位器本身又是一个链表,表中保存着多个由键值对组成的对象的节点。所以这是一个典型的Composite模式的例子,来看它的结构图:
![](https://images.cnblogs.com/cnblogs_com/terrylee/Composite08.jpg)
图6
正如我们在图中所看到的,IReadableLocator定义了最上层的定位器接口方法,它基本上具备了定位器的大部分功能。
部分代码:
public interface IReadableLocator : IEnumerable<KeyValuePair<object, object>>
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//返回定位器中节点的数量
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int Count
{ get; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//一个指向父节点的引用
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
IReadableLocator ParentLocator
{ get; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//表示定位器是否只读
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
bool ReadOnly
{ get; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//查询定位器中是否已经存在指定键值的对象
bool Contains(object key);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//查询定位器中是否已经存在指定键值的对象,根据给出的搜索选项,表示是否要向上回溯继续寻找。
bool Contains(object key, SearchMode options);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//使用谓词操作来查找包含给定对象的定位器
IReadableLocator FindBy(Predicate<KeyValuePair<object, object>> predicate);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//根据是否回溯的选项,使用谓词操作来查找包含对象的定位器
IReadableLocator FindBy(SearchMode options, Predicate<KeyValuePair<object, object>> predicate);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从定位器中获取一个指定类型的对象
TItem Get<TItem>();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从定位其中获取一个指定键值的对象
TItem Get<TItem>(object key);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//根据选项条件,从定位其中获取一个指定类型的对象
TItem Get<TItem>(object key, SearchMode options);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//给定对象键值获取对象的非泛型重载方法
object Get(object key);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//给定对象键值带搜索条件的非泛型重载方法
object Get(object key, SearchMode options);
}
一个抽象基类ReadableLocator用来实现这个接口的公共方法。两个主要的方法实现代码如下:
public abstract class ReadableLocator : IReadableLocator
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 查找定位器,最后返回一个只读定位器的实例
/// </summary>
public IReadableLocator FindBy(SearchMode options, Predicate<KeyValuePair<object, object>> predicate)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
if (!Enum.IsDefined(typeof(SearchMode), options))
throw new ArgumentException(Properties.Resources.InvalidEnumerationValue, "options");
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Locator results = new Locator();
IReadableLocator currentLocator = this;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (currentLocator != null)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FindInLocator(predicate, results, currentLocator);
currentLocator = options == SearchMode.Local ? null : currentLocator.ParentLocator;
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return new ReadOnlyLocator(results);
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 遍历定位器
/// </summary>
private void FindInLocator(Predicate<KeyValuePair<object, object>> predicate, Locator results,
IReadableLocator currentLocator)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (KeyValuePair<object, object> kvp in currentLocator)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!results.Contains(kvp.Key) && predicate(kvp))
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
results.Add(kvp.Key, kvp.Value);
}
}
}
}
可以看到,在FindBy方法里面,循环调用了FindInLocator方法,如果查询选项是只查找当前定位器,那么循环终止,否则沿着定位器的父定位器继续向上查找。FindInLocator方法就是遍历定位器,然后把找到的对象存入一个临时的定位器。最后返回一个只读定位器的新的实例。
从这个抽象基类中派生出一个具体类和一个抽象类,一个具体类是只读定位器(ReadOnlyLocator),只读定位器实现抽象基类没有实现的方法,它封装了一个实现了IReadableLocator接口的定位器,然后屏蔽内部定位器的写入接口方法。另一个继承的是读写定位器抽象类ReadWriteLocator,为了实现对定位器的写入和删除,这里定义了一个对IReadableLocator接口扩展的接口叫做IReadWriteLocator,在这个接口里面提供了实现定位器的操作:
![](https://images.cnblogs.com/cnblogs_com/terrylee/Composite09.jpg)
图7
实现代码如下:
public interface IReadWriteLocator : IReadableLocator
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//保存对象到定位器
void Add(object key, object value);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从定位器中删除一个对象,如果成功返回真,否则返回假
bool Remove(object key);
}
从ReadWirteLocator派生的具体类是Locator类,Locator类必须实现一个定位器的全部功能,现在我们所看到的Locator它已经具有了管理定位器的功能,同时他还应该具有存储的结构,这个结构是通过一个WeakRefDictionary类来实现的,这里就不介绍了。[关于定位器的介绍参考了niwalker的Blog]
效果及实现要点
1.Composite模式采用树形结构来实现普遍存在的对象容器,从而将“一对多”的关系转化“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器。
2.将“客户代码与复杂的对象容器结构”解耦是Composite模式的核心思想,解耦之后,客户代码将与纯粹的抽象接口——而非对象容器的复内部实现结构——发生依赖关系,从而更能“应对变化”。
3.Composite模式中,是将“Add和Remove等和对象容器相关的方法”定义在“表示抽象对象的Component类”中,还是将其定义在“表示对象容器的Composite类”中,是一个关乎“透明性”和“安全性”的两难问题,需要仔细权衡。这里有可能违背面向对象的“单一职责原则”,但是对于这种特殊结构,这又是必须付出的代价。ASP.NET控件的实现在这方面为我们提供了一个很好的示范。
4.Composite模式在具体实现中,可以让父对象中的子对象反向追溯;如果父对象有频繁的遍历需求,可使用缓存技巧来改善效率。
适用性
以下情况下适用Composite模式:
1.你想表示对象的部分-整体层次结构
2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
总结
组合模式解耦了客户程序与复杂元素内部结构,从而使客户程序可以向处理简单元素一样来处理复杂元素。
参考资料
阎宏,《Java与模式》,电子工业出版社
James W. Cooper,《C#设计模式》,电子工业出版社
Alan Shalloway James R. Trott,《Design Patterns Explained》,中国电力出版社
MSDN WebCast 《C#面向对象设计模式纵横谈(9):Composite组合模式(结构型模式)》