组合模式(Composite):将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对象单个对象和组合对象的使用具有一致性。
组合模式结构图:
理解:
1. Component是组合对象的基类,定义了类的公共方法;提供一个访问和管理子组件的方法。
管理子组件的方法:Add-添加子组件;Remove-移除子组件;
2. Leaf是组合对象的叶子节点(树叶)。叶子节点没有子组件(也就没有管理子组件的方法)。
3. Composite是具体的子组件类(树枝)。实现基类接口。它可以继续派生子组件或者叶子节点。
4. Client通过Component基类来操作组合对象。
Component为组合中的对象声明接口(基类),在适当情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component的子部件。
class Component
{
protected:
String name;
public:
virtual void Add(Component *c)=0; //通常都用add和remove方法来提供增加或移//除树叶或树枝的功能
virtual void Remove(Component *c)=0;
virtual void Display(int depth);
};
Leaf在组合中表示叶节点对象,叶节点没有子节点。
class Leaf : public Component
{
public:
Leaf(String name):Component(name){}
void Add(Component * c)
{
cout<< “Cannot add to a leaf”<<endl;
}
void Remove(Component *c)
{
cout<< “Cannot remove from a leaf”<<endl;
}
void Display(int depth)
{
for(int i=0; i<depth;i++)
cout<< “—”;
cout<<name<<endl;
}
};
//由于叶子没有再增加分支和树叶,所以add和remove方法实现它没有意义,但这样做可以消除叶节点和枝节点对象在抽象层次的区别,它们具备完全一致的接口。
Composite定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关操作,比如增加Add和删除Remove
class Composite :public Component
{
private:
list<Component *> children = newlist<Component *>();
public:
Composite(String name):Component(name){}
void Add(Component* c)
{
children.push_back(c);
}
void Remove(Component * c)
{
children.remove(c);
}
void Display(int depth)
{
for(int i=0; i<depth;i++)
cout<< “—”;
cout<<name<<endl;
list<Component *>::iteratorit=children.begin();
for(;it!=children.end();it++)
{
*it->Display(depth+2);
}
}
};
客户端代码,能通过Component接口操作组合部件的对象
void main()
{
Composite *root = new Composite(“root”);
root->Add(new Leaf(“Leaf A”));
root->Add(new Leaf(“Leaf B”));
Composite *comp = new Composite(“CompositeX”);
comp->Add(new Leaf(“Leaf XA”));
comp->Add(new Leaf(“Leaf XB”));
root->Add(comp);
Composite *comp2 = new Composite(“CompositeXY”);
Comp2->Add(new Leaf(“Leaf XYA”));
Comp2->Add(new Leaf(“Leaf XYB”));
comp->Add(comp2);
root->Display(1);
}
结果显示:
—root
———Leaf A
———Leaf B
———Composite X
—————Leaf XA
—————Leaf XA
—————Composite XY
———————Leaf XYA
———————Leaf XYA
何时使用组合模式:
需求中是体现部分与整体层次的结构时,以及希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑用组合模式。