設(shè)計(jì)模式學(xué)習(xí)筆記之十五composite組合模式_第1頁
設(shè)計(jì)模式學(xué)習(xí)筆記之十五composite組合模式_第2頁
設(shè)計(jì)模式學(xué)習(xí)筆記之十五composite組合模式_第3頁
設(shè)計(jì)模式學(xué)習(xí)筆記之十五composite組合模式_第4頁
設(shè)計(jì)模式學(xué)習(xí)筆記之十五composite組合模式_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、設(shè)計(jì)模式 C+學(xué)習(xí)筆記之十五(Comite 組合模式)15.1.解釋概念:將對(duì)象組樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。Com使用具有一致性。ite 使得用戶對(duì)單個(gè)對(duì)象和組合的main(),客戶CCorpNode,抽象基類,實(shí)現(xiàn)基本信息CBranchNode,樹枝節(jié)點(diǎn),實(shí)現(xiàn) Addordinate()函數(shù)和 GetSubordinate()函數(shù)CLeafNode,葉子節(jié)點(diǎn),IsLeaf 屬性總是“true”說明:組合模式主要是實(shí)現(xiàn)在 CBranchNode 對(duì)象里增加對(duì)其它對(duì)象的數(shù)組,如 vector,數(shù)組里可以存放CBranchNode 和CLeafNode 對(duì)象。這樣方便進(jìn)行遍歷操作。注

2、意:組合模式有透明組合模式和安全組合模式。透明組合模式是將Addordinate 和GetSubordinate這兩個(gè)函數(shù)也抽象到CCorpNode 基類里,這增加了操作葉子節(jié)點(diǎn)的難度,更易出現(xiàn)邏輯問題。所以盡量使用安全模式。這個(gè)簡單了,可以想像一下TreeView 和 TreeNode 基本上是這個(gè)意思了,將很多數(shù)據(jù)組織在一塊。看代碼:/CorpNode.h#pragma once #include using std:string; class CCorpNodepublic:CCorpNode();CCorpNode(string _name, string _ virtual CCor

3、pNode(void);virtual string GetInfo();,_salary);void SetParent(CCorpNode *_pParent); CCorpNode * GetParent();virtual bool IsLeaf() = 0; private:string m_name;string m_ition;m_salary; protected:bool m_isLeaf; CCorpNode *m_pParent;/CorpNode.cpp#include StdAfx.h #include CorpNode.h#include .CommonDeclar

4、eConvert.h CCorpNode:CCorpNode(void)m_name = ;m_ition = ;m_salary = 0;CCorpNode:CCorpNode(string _name, string _,_salary) : m_name(_name),m_ition(_), m_salary(_salary)CCorpNode:CCorpNode(void)string CCorpNode:GetInfo()string info = ;info.append(:);info.append(this-m_name);info.append(t 職位:);info.app

5、end(this-m_info.append(t 薪水:);ition);info.append(CConvert:ToString(this-m_salary);return info;void CCorpNode:SetParent( CCorpNode *_parent )this-m_pParent = _parent;CCorpNode * CCorpNode:GetParent()return this-m_pParent;/BranchNode.h#pragma once #include corpnode.h #include CorpNode.h #include #incl

6、ude using std:vector;using std:string; class CBranchNode :public CCorpNodepublic:CBranchNode(void); CBranchNode(string name, string,CBranchNode(void);void Add(CCorpNode *pcorpNode);salary);vector GetSubordinateInfo(); bool IsLeaf();private:vector m_subordinaist;/BranchNode.cpp#include StdAfx.h #incl

7、ude BranchNode.hCBranchNode:CBranchNode(void)m_isLeaf = false;CBranchNode:CBranchNode( string name, string salary)m_isLeaf = false;CBranchNode:CBranchNode(void)void CBranchNode:Add( CCorpNode *pcorpNode )pcorpNode-SetParent(this);,salary ) : CCorpNode(name,m_subordinaist.push_back(pcorpNode);vector

8、CBranchNode:GetSubordinateInfo()return this-m_subordinaist;bool CBranchNode:IsLeaf()return m_isLeaf;/LeafNode.h#pragma once #include corpnode.h class CLeafNode :public CCorpNodepublic:CLeafNode(void); CLeafNode(string name, stringCLeafNode(void); bool IsLeaf();/LeafNode.cpp,salary);#include StdAfx.h #include LeafNode.h CLeafNode:CLeafNode(void)m_isLeaf = true;CLeafNode:CLeafNode( string name, stringm_isLeaf = true;CLeafNode:CLeafNode(void)bool CLeafNode:IsLeaf()return m_isLeaf;,salary ) : CCorpNode(name, salary)曾經(jīng)開發(fā)過一款Gantt 圖的控件,采用的就是這種模式,有GanttView 和GanttNode 兩個(gè)類,IGanttN

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論