設計模式(c++)筆記之九(Composite模式)
阿新 • • 發佈:2019-02-19
問題很多,我們一個一個解決,先說抽象的問題,確實可以吧 IBranch 和 IRoot 合併成一個介面,這個我們先肯定下來,這是個比較大的改動,我們先畫個類圖(類名稍微做下變化):
本人工程目錄:
註釋:
main(),客戶
CCorpNode,抽象基類,實現基本資訊
CBranchNode,樹枝節點,實現Addordinate()函式和GetSubordinate()函式
CLeafNode,葉子節點,IsLeaf屬性總是“true”
說明:組合模式主要是實現在CBranchNode物件裡增加對其它物件的陣列,如vector<CCorpNode*>,數組裡可以存放CBranchNode和CLeafNode物件。這樣方便進行遍歷操作。
注意:組合模式有透明組合模式和安全組合模式。透明組合模式是將Addordinate和GetSubordinate這兩個函式也抽象到CCorpNode基類裡,這增加了操作葉子節點的難度,更易出現邏輯問題。所以儘量使用安全模式。
這個簡單了,可以想像一下TreeView和TreeNode基本上是這個意思了,將很多資料組織在一塊。
程式碼:
抽象基類,實現基本資訊:CorpNode類
CorpNode.h
CorpNode.cpp#ifndef __Composite__CorpNode__ #define __Composite__CorpNode__ #include <iostream> using std::string; class CCorpNode { public: CCorpNode(void); CCorpNode(string _name,string _pos,string _salary); virtual ~CCorpNode(void); virtual string GetInfo(); void SetParent(CCorpNode *_pParent); CCorpNode *GetParent(); virtual bool IsLeaf() = 0; private: string m_name; string m_position; string m_salary; protected: bool m_isLeaf; CCorpNode *m_pParent; }; #endif /* defined(__Composite__CorpNode__) */
#include "CorpNode.h" CCorpNode::CCorpNode(void) { m_name = ""; m_position = ""; m_salary = "0"; } CCorpNode::CCorpNode(string _name,string _pos,string _salary):m_name(_name), m_position(_pos), m_salary(_salary) { } CCorpNode::~CCorpNode(void) { } string CCorpNode::GetInfo() { string info = ""; info.append("姓名: "); info.append(this->m_name); info.append("\t職位:"); info.append(this->m_position); info.append("\t薪水:"); info.append(this->m_salary); return info; } void CCorpNode::SetParent(CCorpNode *_pParent) { this->m_pParent = _pParent; } CCorpNode * CCorpNode::GetParent() { return this->m_pParent; }
樹枝節點:BranchNode類
BranchNode.h
#ifndef __Composite__BranchNode__
#define __Composite__BranchNode__
#include <iostream>
#include "CorpNode.h"
#include <vector>
using std::vector;
using std::string;
class CBranchNode:public CCorpNode
{
public:
CBranchNode(void);
CBranchNode(string name,string pos,string salary);
~CBranchNode(void);
void Add(CCorpNode* pcorpNode);
vector<CCorpNode*> GetSubordinateInfo();
bool IsLeaf();
private:
vector<CCorpNode*> m_subordinateList;
};
#endif /* defined(__Composite__BranchNode__) */
BranchNode.cpp#include "BranchNode.h"
CBranchNode::CBranchNode(void)
{
m_isLeaf = false;
}
CBranchNode::CBranchNode(string name ,string pos,string salary):CCorpNode(name,pos,salary)
{
m_isLeaf = false;
}
CBranchNode::~CBranchNode(void)
{
}
void CBranchNode::Add(CCorpNode *pcorpNode)
{
pcorpNode->SetParent(this);
m_subordinateList.push_back(pcorpNode);
}
vector<CCorpNode*> CBranchNode::GetSubordinateInfo()
{
return this->m_subordinateList;
}
bool CBranchNode::IsLeaf()
{
return m_isLeaf;
}
葉子節點:LeafNode類
LeafNode.h
#ifndef __Composite__LeafNode__
#define __Composite__LeafNode__
#include <iostream>
#include "CorpNode.h"
class CLeafNode:public CCorpNode
{
public:
CLeafNode(void);
CLeafNode(string name,string pos,string salary);
~CLeafNode(void);
bool IsLeaf();
};
#endif /* defined(__Composite__LeafNode__) */
LeafNode.cpp#include "LeafNode.h"
CLeafNode::CLeafNode(void)
{
m_isLeaf = true;
}
CLeafNode::CLeafNode(string name ,string pos,string salary):CCorpNode(name,pos,salary)
{
m_isLeaf = true;
}
CLeafNode::~CLeafNode(void)
{
}
bool CLeafNode::IsLeaf()
{
return m_isLeaf;
}
客戶:main主程式
main.cpp
#include <stdio.h>
#include "CorpNode.h"
#include "BranchNode.h"
#include "LeafNode.h"
#include "CConvert.h"
using std::cout;
string getTreeInfo(CBranchNode *root)
{
vector<CCorpNode*> subordinateList = root->GetSubordinateInfo();
string info = "";
for (vector<CCorpNode*>::iterator it = subordinateList.begin();it != subordinateList.end();it++)
{
if ((*it)->IsLeaf())
{
info = info.append(" "+(*it)->GetInfo()+"\n");
}
else
{
info = info.append((*it)->GetInfo()+"\n"+getTreeInfo((CBranchNode*)(*it)));
}
}
return info;
}
CBranchNode *compositeCorpTree()
{
//首先產生總經理CEO
CBranchNode *root = new CBranchNode("王大麻子","總經理","100000");
//把三個部門經理產生出來
CBranchNode *developDep = new CBranchNode("劉大瘸子","研發部門經理","10000");
CBranchNode *salesDep = new CBranchNode("馬二柺子","銷售部門經理","20000");
CBranchNode *financeDep = new CBranchNode("趙三駝子","財務部經理","10000");
//再把三個小組長產生出來
CBranchNode *firstDevGroup = new CBranchNode("楊三乜斜","開發一組組長","5000");
CBranchNode *secondDevGroup = new CBranchNode("吳大棒槌","開發二組組長","6000");
//把所有的小兵都產生出來
CLeafNode *a = new CLeafNode("a","開發人員","2000");
CLeafNode *b = new CLeafNode("b","開發人員","2000");
CLeafNode *c = new CLeafNode("c","開發人員","2000");
CLeafNode *d = new CLeafNode("d","開發人員","2000");
CLeafNode *e = new CLeafNode("e","開發人員","2000");
CLeafNode *f = new CLeafNode("f","開發人員","2000");
CLeafNode *g = new CLeafNode("g","開發人員","2000");
CLeafNode *h = new CLeafNode("h","銷售人員","2000");
CLeafNode *i = new CLeafNode("i","銷售人員","2000");
CLeafNode *j = new CLeafNode("i","財務人員","2000");
CLeafNode *k = new CLeafNode("k","CEO祕書","2000");
CLeafNode *zhengLaoLiu = new CLeafNode("鄭老六","研發部副經理","2000");
//開始組裝
//CEO下有三個部門經理和一個祕書
root->Add(developDep);
root->Add(salesDep);
root->Add(financeDep);
root->Add(k);
//研發部經理
developDep->Add(zhengLaoLiu);
developDep->Add(firstDevGroup);
developDep->Add(secondDevGroup);
//看看開發兩個開發小組下有什麼
firstDevGroup->Add(a);
firstDevGroup->Add(b);
firstDevGroup->Add(c);
secondDevGroup->Add(d);
secondDevGroup->Add(e);
secondDevGroup->Add(f);
//再看銷售部下的人員情況
salesDep->Add(h);
salesDep->Add(i);
//最後一個財務
firstDevGroup->Add(j);
return root;
delete zhengLaoLiu,k,j,i,h,g,f,e,d,c,b,a,secondDevGroup,firstDevGroup,financeDep,salesDep,developDep,root;
}
int main(int argc, const char * argv[])
{
CBranchNode *CEO = compositeCorpTree();
cout<<CEO->GetInfo()<<std::endl;
cout<< getTreeInfo(CEO);
// insert code here...
//printf("Hello, World!\n");
return 0;
}
結果如下:
參考文獻:《設計模式之禪》,《GoF_23種設計模式解析》
參考部落格:http://www.cnblogs.com/wanggary/archive/2011/04/19/2021638.html