对象容器的问题
public interface IBox
{
public void Process();
}
public class SigleBox : IBox
{
public void Process(){...}
}
public class ContainBox : IBox
{
public void Process(){...}
public ArrayList getBoxes(){...}
}
解决方法
IBox box = Factory.GetBox();
if( box is ContainBox)
{
box.Process();
ArrayList list = ((ContainBox)box).getBoxes();
...//将面临比较复杂的递归
}
else
{
box.Process();
}
1、动机
上述描述的问题根源在于:客户代码过多地依赖于对象容器复杂的内部实现结构,对象容器内部实现结构(而非抽象接口)的变化将引起客户代码的频繁变化带来了代码的维护性、扩展性等弊端。
如何将“客户代码与复杂的容器内部结构”解耦,让对象容器自己来实现自身的复杂结构,从而使得客户代码就像处理简单对象一样来处理复杂的对象容器?
2、意图
将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性。
3、图
4、代码
安全式模式
public interface Component
{
void SampleOperation();
}
public class Leaf : Component
{
public void SampleOperation()
{
...
}
}
public class Composite : Component
{
ArrayList list = null;
public void Add(Component component)
{
if ( list == null)
{
list = new ArrayList();
}
list.Add(component);
}
public void Remove(Component component)
{
if ( list == null)
{
throw new xxxException();
}
list.Remove(component);
}
public void SampleOperation()
{
//1 Do SampleOperationfor myself
//2 Do SampleOperation for the box in the list
if ( list != null)
{
foreach(Component component in list)
{
component.SampleOperation();
}
}
}
}
透明式模式
public interface Component
{
void Add(Component component);
void Remove(Component component);
void SampleOperation();
}
public class Leaf : Component
{
public void SampleOperation()
{
...
}
public void Add(Component component)
{
}
public void Remove(Component component)
{
}
}
public class Composite : Component
{
ArrayList list = null;
public void Add(Component component)
{
if ( list == null)
{
list = new ArrayList();
}
list.Add(component);
}
public void Remove(Component component)
{
if ( list == null)
{
throw new xxxException();
}
list.Remove(component);
}
public void SampleOperation()
{
//1 Do SampleOperationfor myself
//2 Do SampleOperation for the box in the list
if ( list != null)
{
foreach(Component component in list)
{
component.SampleOperation();
}
}
}
}
public interface IBox
{
public void Process();
}
public class SigleBox : IBox
{
public void Process(){...}
}
public class ContainerBox : IBox
{
ArrayList list = null;
public void Add(IBox box)
{
if ( list == null)
{
list = new ArrayList();
}
list.Add(box);
}
public void Remove(IBox box)
{
if ( list == null)
{
throw new xxxException();
}
list.Remove(box);
}
public void Process()
{
//1 Do Process for myself
//2 Do Process for the box in the list
if ( list != null)
{
foreach(IBox box in list)
{
box.Process();
}
}
}
}
class App
{
public static void main()
{
IBox box = Factory.GetBox();
box.Process();
}
}
4、要点
(1) Composite模式采用树形结构来实现普遍存在的对象容器,从而将“一对多”的关系转化为“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器。
(2) 将“客户代码与复杂的对象容器结构”解耦是Composite模式的核心思想,解耦之后,客户代码将与纯粹的对象接口——而非对象容器的复杂内部实现结构——发生依赖关系,从而更能“应对变化”。
(3) Composite模式中,是将“Add”和“Remove”等和对象容器相关的方法定义在“表示抽象对象的的component类”中,还是将其定义在“表示对象容器的composite类”中,是一个关乎“透明性”和“安全性”的两难问题,需要仔细权衡。这里有可能违背面向对象的“单一职责原则”,但是对于这种特殊结构,这又是必须付出的代价。Asp.Net控件的实现在这方面为我们提供了一个很好的示范。
(4) Composite模式在具体实现中,可以让父对象中的子对象反向追溯;如果父对象有频繁的遍历需求,可使用缓存技术来改善效率。