组合模式(Composition)
组合模式(Composition)
意图:将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。
应用:组合图形、文件目录、GUI容器等。
模式结构:
心得:
用户(Client)通过抽象类(Component)提供的公用接口统一操作基本对象(Leaf)和组合对象(Composite)。抽象对象应该拥有操作基本对象和组合对象的所有接口,但是基本对象应该屏蔽某些组合对象特有的操作,如添加删除等。组合对象聚合了多个抽象对象,这种一对多的关系和继承关系正好描述了一种树形的组合结构。
举例:
抽象对象看作抽象出来的文件,Leaf看作具体的文件,Composite看作文件夹。那么这个模式描述的是用户通过操纵一个抽象的文件对象,而不用关系这个对象是文件还是文件夹。文件对象是不允许添加子文件操作的,文件夹对象可以包含多个抽象的文件,即可以是文件也可以子文件夹。按照上述设计结构,C++实现如下:
{
public:
virtual void operation()= 0;
virtual void add(Component*)= 0;
virtual void remove(Component*)= 0;
virtual Component* getChild( int i)= 0;
virtual ~Component(){}
};
class Composite: public Component
{
list<Component*> children;
public:
virtual void operation()
{
cout<< " 组合对象( ";
for(list<Component*>::iterator it=children.begin();it!=children.end();++it)
{
(*it)->operation();
cout<< " ";
}
cout<< " ) ";
}
virtual void add(Component*pc)
{
children.push_back(pc);
}
virtual void remove(Component*pc)
{
children.remove(pc);
}
virtual Component* getChild( int i)
{
list<Component*>::iterator it;
for(it=children.begin();it!=children.end()&&(i> 0);++it,--i);
return *it;
}
virtual ~Composite()
{
for(list<Component*>::iterator it=children.begin();it!=children.end();++it)
{
delete *it;
}
}
};
class Leaf: public Component
{
public:
virtual void operation()
{
cout<< " 基本对象 ";
}
virtual void add(Component*pc){}
virtual void remove(Component*pc){}
virtual Component* getChild( int i){ return NULL;}
};
如果要初始化下边结构的文件目录结构并调用操作operation,用户可以这么操作:
pc1->add( new Leaf());
Component*pc2= new Composite();
pc2->add( new Leaf());
pc1->add(pc2);
pc1->operation();
delete pc1;
代码中需要注意的地方是,抽象基类必须重定义虚析构函数,否则通过基类Component指针无法删除派生类Composite对象。
使用组合模式的好处从代码中可以看出来,我们可以对顶层组合对象直接进行调用operation操作,而不需要关心它的内部结构,就像操作Leaf对象一样,增大的代码的灵活性。