設計模式——組合模式(C++實現)

來源:互聯網
上載者:User

標籤:delete   str   isp   osi   表示   pre   技術分享   http   from   

組合模式:將對象組合成樹形結構以表示“部分-整體”的階層。組合模式使得使用者對單個對象和組合對象的使用具有一致性。 是一種結構型模式 

 

 使用情境:1、用於對象的部分-整體階層,如樹形菜單、檔案夾菜單、部門組織架構圖等;2、對使用者隱藏組合對象與單個對象的不同,使得使用者統一地使用組合結構中的所有對象。

 

  1 #include <iostream>  2 #include <string>  3 #include <vector>  4   5 using namespace std;  6   7 class STComponent  8 {  9 public: 10         STComponent() 11         { 12  13         } 14         STComponent(string strName): m_strName(strName) 15         { 16  17         } 18         virtual ~STComponent() 19         { 20  21         } 22  23         /* 24         virtual void Add(STComponent* c); 25         virtual void Remove(STComponent* c) ; 26         virtual void Display(int iDepth); 27         */ 28  29  30         virtual void Add(STComponent* c) = 0; 31         virtual void Remove(STComponent* c) = 0; 32         virtual void Display(int iDepth) = 0; 33  34         string m_strName; 35  36 }; 37  38 class STLeaf: public STComponent 39 { 40 public: 41         STLeaf(string strName): STComponent(strName) 42         { 43  44         } 45  46         virtual void Add(STComponent* c) 47         { 48                 cout<< "Cann‘t Add to a leaf"<< endl; 49         } 50         virtual void Remove(STComponent* c) 51         { 52                 cout<< "Cann‘t Remove from a leaf"<< endl; 53         } 54         virtual void Display(int iDepth) 55         { 56                 cout<< string(iDepth, ‘-‘)<< m_strName<< endl; 57         } 58 }; 59  60 class STComposite: public STComponent 61 { 62 public: 63         STComposite(string strName): STComponent(strName) 64         { 65  66         } 67         ~STComposite() 68         { 69                 m_vecStComposite.clear(); 70         } 71  72         virtual void Add(STComponent* c) 73         { 74                 m_vecStComposite.push_back(c); 75         } 76  77         virtual void Remove(STComponent* c) 78         { 79                 for (typeof(m_vecStComposite.begin()) it = m_vecStComposite.begin(); it != m_vecStComposite.end();) 80                 { 81                         if (*it == c) 82                         { 83                                 it = m_vecStComposite.erase(it); 84                                 cout<< "erase Succ: "<< (*it)->m_strName<< endl; 85                         } 86                         else 87                         { 88                                 ++it; 89                         } 90                 } 91         } 92  93         virtual void Display(int iDepth) 94         { 95                 cout<< string(iDepth, ‘-‘)<< m_strName<< endl; 96                 for (size_t i = 0; i < m_vecStComposite.size(); ++i) 97                 { 98                         m_vecStComposite[i]->Display(iDepth+1); 99                 }100         }101 102         vector<STComponent*> m_vecStComposite;103 };104 105 int main(int argc, char* argv[])106 {107         //STLeaf* pstLeaf = new STLeaf("leafA");108         //pstLeaf->Add(NULL);109         //pstLeaf->Remove(NULL);110         //pstLeaf->Display(10);111         //delete pstLeaf;112 113         STComposite* root = new STComposite("root");114         root->Add(new STLeaf("Leaf A"));115         root->Add(new STLeaf("Leaf B"));116 117         STComposite* comp = new STComposite("Composite X");118         comp->Add(new STLeaf("Leaf XA"));119         comp->Add(new STLeaf("Leaf XB"));120         root->Add(comp);121 122         STComposite* comp2 = new STComposite("Composite XY");123         comp2->Add(new STLeaf("Leaf XYA"));124         comp2->Add(new STLeaf("Leaf XYB"));125         comp->Add(comp2);126 127         STLeaf* pstLeaf = new STLeaf("leaf D");128         root->Add(pstLeaf);129         root->Display(1);130 131 //      root->Remove(pstLeaf);132         root->Remove(comp);133         root->Display(1);134 135         return 0;136 }137 /////////////////////////////138 [[email protected] ~/learn_code/design_pattern/16_composite]$ ./composite139 -root140 --Leaf A141 --Leaf B142 --Composite X143 ---Leaf XA144 ---Leaf XB145 ---Composite XY146 ----Leaf XYA147 ----Leaf XYB148 --leaf D149 erase Succ: leaf D150 -root151 --Leaf A152 --Leaf B153 --leaf D

 

設計模式——組合模式(C++實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.