设计模式之组合模式

来源:互联网 发布:电气常用数据手册 编辑:程序博客网 时间:2024/05/22 15:02

1、组合模式(Composite):将对象组合成树形结构已表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

UML图如下:



2、组合模式的透明方式与安全方式

1)透明方法:

也就是说在Component中声明所有用来管理子对象的方法,其中包括Add,Remove等。这样实现Component接口所有的子类都具备了Add和Remove。

这样做的好处就是叶节点和枝节点对于外界没有区别,它们具备完全一致的行为接口,但问题也很明显。因为Leaf类本身不具备Add(),Remove方法的功能,所以实现它是没有意义的。

2)安全方法:

也就是在Component接口中不去声明Add和Remove方法,那么子类的Leaf也就不需要去实现它,而是在Composite声明所有用来管理子类对象的方法,这样的做就不会出现刚才提出的问题,由于不够透明,所以树叶和树枝类将不具有相同的接口,客户端的调用需要做相应的判断,带来了不便。


3、如何使用组合模式?

答:当你发现需求中是体系部分与整体层次结构时,以及你希望用户可以忽略组合对象与单个对象的的不同,统一使用组合结构中的所有对象时,就应该考虑用组合模式了。

4、组合模式的好处?

答:组合模式定义了包含基本对象,组合对象的类层次结构,基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,这样不断地递归下去,客户代码中,任何用到基本对象的地方都可以使用组合对象。用户不用关心到底是处理一个叶节点还是处理一个组合组件。也就用不着为定义组合而写一些选择判断语句了。组合模式让客户可以一致地使用组合结构和单个对象。


5、C++代码实现

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <vector>  
  4. #include <algorithm>  
  5. using namespace std;  
  6.   
  7. /* Component为组合中的对象声明接口,在适当情况下, 
  8.     实现所有类共有接口的默认行为。声明一个接口用于 
  9.     访问和管理Component的子部件 */  
  10. class Component  
  11. {  
  12. protected:  
  13.     string name;  
  14. public:  
  15.     Component(string);  
  16.     /* 通常都用Add和Remove的方法 
  17.         来提供增加和移除树叶或树枝的功能*/  
  18.     virtual void Add(Component *) = 0;  
  19.     virtual void Remove(Component *) = 0;  
  20.     virtual void Display(int) = 0;  
  21. };  
  22.   
  23. Component::Component(string name)   
  24. {  
  25.     Component::name = name;  
  26. }  
  27.   
  28. /* Leaf在组合中表示叶节点对象,叶节点没有子节点。 */  
  29. class Leaf : public Component  
  30. {  
  31. public:  
  32.     Leaf(string name):Component(name) {}  
  33.     void Add(Component *);  
  34.     void Remove(Component *);  
  35.     void Display(int);  
  36. };  
  37.   
  38. /* 由于叶子没有增加分枝和树叶,所以Add和Remove方法实现它没有意义, 
  39.     但这样做可以消除叶节点和枝节点对象在抽象层次的区别,它们具有 
  40.     完全一致的接口。 */  
  41. void Leaf::Add(Component * p_c)  
  42. {  
  43.     cout << "Cannot add to a leaf" << endl;  
  44. }  
  45.   
  46. void Leaf::Remove(Component * p_c)  
  47. {  
  48.     cout << "Cannot remove from a leaf" << endl;  
  49. }  
  50.   
  51. /* 叶子节点的具体方法,此处是显示器名称和级别 */  
  52. void Leaf::Display(int depth)  
  53. {  
  54.     for (int i = 0; i < depth; ++i) {  
  55.         cout << "-";  
  56.     }  
  57.     cout << name << endl;  
  58. }  
  59.   
  60. /*  Component定义有枝节点行为,用来存储子部件 
  61.     在Component接口中实现与子部件有关的操作, 
  62.    比如增加Add和删除Remove。 */  
  63. class Composite : public Component  
  64. {  
  65. private:  
  66.     vector<Component *> v;  
  67. public:  
  68.     Composite(string name) : Component(name) {}  
  69.     void Add(Component *);  
  70.     void Remove(Component *);  
  71.     void Display(int);  
  72. };  
  73.   
  74. void Composite::Add(Component * p_c)  
  75. {  
  76.     v.push_back(p_c);  
  77. }  
  78.   
  79. void Composite::Remove(Component * p_c)  
  80. {  
  81.     vector<Component *>::iterator it;  
  82.     it = find(v.begin(), v.end(), p_c);  
  83.     v.erase(it);  
  84. }  
  85.   
  86. /* 显示节点名称并对其下级进行遍历  */  
  87. void Composite::Display(int depth)  
  88. {  
  89.     for (int i = 0; i < depth; ++i) {  
  90.         cout << "-";  
  91.     }  
  92.     cout << name << endl;  
  93.     int cnt = v.size();  
  94.     for (int i = 0; i < cnt; ++i) {  
  95.         v[i]->Display(depth + 2);  
  96.     }  
  97. }  
  98.   
  99. int main()  
  100. {  
  101.     Composite * root = new Composite("root");  
  102.     Leaf * leafA = new Leaf("Leaf A");  
  103.     root->Add(leafA);  
  104.     root->Add(new Leaf("Leaf B"));  
  105.   
  106.     Composite * comp = new Composite("Composite X");  
  107.     comp->Add(new Leaf("Leaf XA"));  
  108.     comp->Add(new Leaf("Leaf XB"));  
  109.     root->Add(comp);  
  110.     root->Display(1);  
  111.   
  112.     root->Remove(leafA);  
  113.     root->Display(1);  
  114.     return 0;  
  115. }  

0 0