动机:
客户代码过多地依赖于对象容器复杂的内部实现结构,对象容器内部实现结构(而非抽象接口)的变化将客户代码的频繁变化,带来了代码的维护性差、扩展性差等弊端。
如何将“客户代码与复杂的对象容器结构”解耦?让对象容器自己来实现自身的复杂结构,从而使得客户代码就像处理简单对象一样来处理复杂的对象容器?
意图:
将对象组合成结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性。
出自:《设计模式》GoF
Composite模式的几个要点:
1、Composite模式采用结构来实现普遍存在的对象容器,从而将“一对多”的关系转化为“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器。
2、将“客户代码与复杂的对象容器结构”解耦是Composite模式的核心思想,解耦之后,客户代码将与纯粹的抽象接口--而非对象容器的内部实现结构--发生依赖关系,从而更能“应对变化”。
3、Composite模式中,是将“Add和Remove等和对象容器相关的方法”定义在“表示抽象对象的Component类”中,还是将其定义在“表示对象容器的 Composite类”中,是一个关乎“透明性”和“安全性”的两难问题,需要仔细权衡。这里有可能违背面向对象的“单一职责原则”,但是对于这种特殊结构,这又是必须付出的代价。ASP.NET控件的实现在这方面为我们提供了一个很好的示范不。
4、Composite模式在具体实现中,可以让父对象中的子对象反向追溯,如果父对象有频繁的遍历需求,可使用缓存技巧来改善效率。
稳定部分:
//实现A:
public interface IBoxA
{
public void Process();
public void Add(Ibox box);
public void Remove(Ibox box);
}
//实现B:
public interface IBoxB
{
public void Process();
public IList Boxes{get;set;}
}
public interface IBoxA
{
public void Process();
public void Add(Ibox box);
public void Remove(Ibox box);
}
//实现B:
public interface IBoxB
{
public void Process();
public IList Boxes{get;set;}
}
变化部分:
//实现A:
//叶子
public class SingleBoxA : IBoxA
{
public void Process()
{
//1.Do process for myself
//
}
}
//树叉
public class ContainerBoxA : IBoxA
{
public void Add(IBoxA box)
{
if (list == null)
{
list = new ArrayList();
}
list.Add(box);
}
public void Remove(IBoxA box)
{
if (list == null)
{
throw new Exception();
}
list.Remove();
}
public void Process()
{
//1.Do process for myself
//.
//2.Do process for the box in the list
if (list != null)
{
foreach (IBoxA box in list)
{
box.Process();
}
}
}
}
//实现B:
//叶子
public class SingleBoxB : IBoxB
{
//IList Boxes
public void Process()
{
//1.Do process for myself
//
}
}
//树叉
public class ContainerBoxB : IBoxB
{
//IList Boxes
public void Process()
{
//1.Do process for myself
//.
//2.Do process for the box in the list
if (this.Boxes != null)
{
foreach (IBoxB box in this.Boxes)
{
box.Process();
}
}
}
}
//叶子
public class SingleBoxA : IBoxA
{
public void Process()
{
//1.Do process for myself
//
}
}
//树叉
public class ContainerBoxA : IBoxA
{
public void Add(IBoxA box)
{
if (list == null)
{
list = new ArrayList();
}
list.Add(box);
}
public void Remove(IBoxA box)
{
if (list == null)
{
throw new Exception();
}
list.Remove();
}
public void Process()
{
//1.Do process for myself
//.
//2.Do process for the box in the list
if (list != null)
{
foreach (IBoxA box in list)
{
box.Process();
}
}
}
}
//实现B:
//叶子
public class SingleBoxB : IBoxB
{
//IList Boxes
public void Process()
{
//1.Do process for myself
//
}
}
//树叉
public class ContainerBoxB : IBoxB
{
//IList Boxes
public void Process()
{
//1.Do process for myself
//.
//2.Do process for the box in the list
if (this.Boxes != null)
{
foreach (IBoxB box in this.Boxes)
{
box.Process();
}
}
}
}
主程序:
public static void Main()
{
IBoxA box = Factory.GetBox();
//因为SingleBox和ContainerBox对客户来说是透明的。可以用下面的调用来处理
box.Process();
// //否则我们要这样处理,客户代码和对象内部结构发生了耦合
// if (box is ContainerBoxA)
// {
// box.Process();
// ArrayList list = ((ContainerBoxA)box).GetBoxes();
// //下面将面临比较复杂的递归处理
// }
// else if (box is SingleBoxA)
// {
// box.Process();
// }
}
{
IBoxA box = Factory.GetBox();
//因为SingleBox和ContainerBox对客户来说是透明的。可以用下面的调用来处理
box.Process();
// //否则我们要这样处理,客户代码和对象内部结构发生了耦合
// if (box is ContainerBoxA)
// {
// box.Process();
// ArrayList list = ((ContainerBoxA)box).GetBoxes();
// //下面将面临比较复杂的递归处理
// }
// else if (box is SingleBoxA)
// {
// box.Process();
// }
}