[cpp] view plain copy
 
 print?
  1. /* 
  2.     组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用用户对单个对象和 
  3.     组合对象的使用具有一致性。 
  4.     Created by Phoenix_FuliMa 
  5. */  
  6.   
  7. #include <iostream>  
  8. #include <string>  
  9. #include <list>  
  10. using namespace std;  
  11.   
  12. class Component  
  13. {  
  14. protected:  
  15.     string name;  
  16. public:  
  17.     Component(string name)  
  18.         :name(name)  
  19.     {   }  
  20.     virtual void AddComponent(Component *component) {   }  
  21.     virtual void RemoveComponent(Component *component)  {   }  
  22.     virtual void GetChild(int depth)    { }  
  23. };  
  24.   
  25. class Leaf: public Component  
  26. {  
  27. public:  
  28.     Leaf(string name)  
  29.         :Component(name)  
  30.     {   }  
  31.     void AddComponent(Component *component)  
  32.     {  
  33.         cout<<"Leaf can't add component"<<endl;  
  34.     }  
  35.     void RemoveComponent(Component *component)  
  36.     {  
  37.         cout<<"Leaf can't remove component"<<endl;  
  38.     }  
  39.     void GetChild(int depth)  
  40.     {  
  41.         string _tmpstring(depth, '-');  
  42.         cout<<_tmpstring<<name<<endl;  
  43.     }  
  44. };  
  45.   
  46. class Composite:public Component  
  47. {  
  48. private:  
  49.     list<Component*> _componets;  
  50.   
  51. public:  
  52.     Composite(string name)  
  53.         :Component(name)  
  54.     { }  
  55.     void AddComponent(Component *component)  
  56.     {  
  57.         _componets.push_back(component);  
  58.     }  
  59.     void RemoveComponent(Component *component)  
  60.     {  
  61.         _componets.remove(component);  
  62.     }  
  63.     void GetChild(int depth)  
  64.     {  
  65.         string tmpstring (depth, '-');  
  66.         cout<<tmpstring<<name<<endl;  
  67.         list<Component*>::iterator iter = _componets.begin();  
  68.         for(; iter != _componets.end(); iter++)  
  69.         {  
  70.             (*iter)->GetChild(depth + 2);  
  71.         }  
  72.     }  
  73. };  
  74.   
  75. int main()  
  76. {  
  77.     Composite *root = new Composite("root");  
  78.     Leaf *leaf1 = new Leaf("leaf1");  
  79.     Leaf *leaf2 = new Leaf("leaf2");  
  80.     root->AddComponent(leaf1);  
  81.     root->AddComponent(leaf2);  
  82.   
  83.     Composite *lay2 = new Composite("layer2");  
  84.     Leaf *leaf4 = new Leaf("leaf4");  
  85.     lay2->AddComponent(leaf4);  
  86.   
  87.     Composite *lay1 = new Composite("layer1");  
  88.     Leaf *leaf3 = new Leaf("leaf3");  
  89.     lay1->AddComponent(leaf3);  
  90.     lay1->AddComponent(lay2);  
  91.   
  92.     root->AddComponent(lay1);  
  93.   
  94.     root->GetChild(1);  
  95.     cout<<endl;  
  96.     lay1->GetChild(1);  
  97.     cout<<endl;  
  98.     lay2->GetChild(1);  
  99.   
  100.     delete root;  
  101.     delete lay1;  
  102.     delete lay2;  
  103.     delete leaf1;  
  104.     delete leaf2;  
  105.     delete leaf3;  
  106.     delete leaf4;  
  107.     system("pause");  
  108.     return 0;  
  109. }