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

来源:http://www.cnblogs.com/070412-zwc/archive/2017/05/19/6880146.html
-Advertisement-
Play Games

組合模式:將對象組合成樹形結構以表示“部分-整體”的層次結構。 組合模式使得用戶對單個對象和組合對象的使用具有一致性。 是一種結構型模式 使用場景: 1、用於對象的部分-整體層次結構,如樹形菜單、文件夾菜單、部門組織架構圖等; 2、對用戶隱藏組合對象與單個對象的不同,使得用戶統一地使用組合結構中的所 ...


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

 

  使用場景: 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 [root@ ~/learn_code/design_pattern/16_composite]$ ./composite
139 -root
140 --Leaf A
141 --Leaf B
142 --Composite X
143 ---Leaf XA
144 ---Leaf XB
145 ---Composite XY
146 ----Leaf XYA
147 ----Leaf XYB
148 --leaf D
149 erase Succ: leaf D
150 -root
151 --Leaf A
152 --Leaf B
153 --leaf D

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近在學習PHP的文件讀寫時,感覺很多資料對文件打開模式說得不很清楚,特別是關於r+,w+,a+三者的區別,對文件指針的概念也說的很模糊。所以我就將自己學習歷程系統寫了下來。本文包括:一.實現文件讀取和寫入的基本思路 二.使用fopen方法打開文件 三.文件讀取和文件寫入操作 四.使用fclose方... ...
  • 相信有不少的小伙伴們遇到過這種事情,安裝在某盤的wamp由於該盤的剩餘空間不足了,而此時自己即將要開發某個網頁項目,並且自己預估項目文件的大小將超出該盤的剩餘空間大小(或許因為項目文件中要包括大量的視頻文件),這就讓我們小伙伴們很尷尬了,既不想將當前在wamp中www的目錄下的工程項目文件轉移到其他 ...
  • 一、為什麼要使用Python進行數據分析? python擁有一個巨大的活躍的科學計算社區,擁有不斷改良的庫,能夠輕鬆的集成C,C++,Fortran代碼(Cython項目),可以同時用於研究和原型的構建以及生產系統的構建。 二、Python的優勢與劣勢: 1.Python是一種解釋型語言,運行速度比 ...
  • 線性回歸、對數幾率回歸模型,本質上是單個神經元。計算輸入特征加權和。偏置視為每個樣本輸入特征為1權重,計算特征線性組合。激活(傳遞)函數 計算輸出。線性回歸,恆等式(值不變)。對數幾率回歸,sigmoid。輸入->權重->求和->傳遞->輸出。softmax分類含C個神經元,每個神經元對應一個輸出類 ...
  • CV_EXPORTS_W void approxPolyDP( InputArray curve, OutputArray approxCurve, double epsilon, bool closed ); @param curve Input vector of a 2D point stor ...
  • 設計模式零 一、設計模式分類 創建型:創建對象的模式 結構型:討論類和對象的結構 行為型:關註對象的行為,解決對象之間的聯繫問題。 二、設計原則 2.1 單一職責原則 只有一個引起它變化的原因,一個類只有一個職責。 2.2 里氏替換原則 父類能出現的地方能用子類替換,但是反過來不一定可以。 2.3 ...
  • 上節談了談類工廠/對象查找服務,本節談談AOP的實現。 AOP為Aspect Oriented Programming的縮寫,意為:面向切麵編程,通過預編譯方式和運行期動態代理實現程式功能的統一維護的一種技術。 Netop.Core的AOP採用代理的實現方式。採用代理方式,您的類就必須繼承一個基類( ...
  • 截止目前,筆者在博客園上面已經發表了3篇關於網路下載的文章,這三篇博客實現了基於socket的http多線程遠程斷點下載實用程式。筆者打算在此基礎上開發出一款分散式文件管理實用程式,截止目前,已經實現了 服務端/客戶端 的上傳、下載部分的功能邏輯。涉及到的知識點包括線程池技術、linux epoll... ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...