头文件:
using namespace std;
/*
*
* 将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合的使用具有一致性。
说明:组合模式主要是实现在CBranchNode对象里增加对其它对象的数组,如vector
*/
class CCompositeMode
{
public:
CCompositeMode();
};
class CCorpNode
{
public:
CCorpNode();
CCorpNode(string _name, string _pos, int _salary);
virtual ~CCorpNode(void);
virtual string GetInfo();
void SetParent(CCorpNode *_pParent);
CCorpNode * GetParent();
virtual bool IsLeaf() = 0;
private:
string m_name;
string m_position;
int m_salary;
protected:
bool m_isLeaf;
CCorpNode *m_pParent;
};
//树枝节点,实现Addordinate()函数和GetSubordinate()函数
class CBranchNode :
public CCorpNode
{
public:
CBranchNode(void);
CBranchNode(string name, string pos, int salary);
~CBranchNode(void);
void Add(CCorpNode *pcorpNode);
vector<CCorpNode*> GetSubordinateInfo();
bool IsLeaf();
private:
vector<CCorpNode*> m_subordinateList;
};
//叶子节点
class CLeafNode :
public CCorpNode
{
public:
CLeafNode(void);
CLeafNode(string name, string pos, int salary);
~CLeafNode(void);
bool IsLeaf();
};
int CompositeTest();
// CCOMPOSITEMODE_H
源文件:
CCompositeMode::CCompositeMode()
{
}
CCorpNode::CCorpNode(void)
{
m_name = "";
m_position = "";
m_salary = 0;
}
CCorpNode::CCorpNode(string _name, string _pos, int _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薪水:");
char buf[50] = {0};
sprintf_s(buf, "%d", this->m_salary);
info.append(buf);
return info;
}
void CCorpNode::SetParent( CCorpNode *_parent )
{
this->m_pParent = _parent;
}
CCorpNode * CCorpNode::GetParent()
{
return this->m_pParent;
}
CBranchNode::CBranchNode(void)
{
m_isLeaf = false;
}
CBranchNode::CBranchNode( string name, string pos, int 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;
}
CLeafNode::CLeafNode(void)
{
m_isLeaf = true;
}
CLeafNode::CLeafNode( string name, string pos, int salary ) : CCorpNode(name, pos, salary)
{
m_isLeaf = true;
}
CLeafNode::~CLeafNode(void)
{
}
bool CLeafNode::IsLeaf()
{
return m_isLeaf;
}
//使用测试
string GetTreeInfo(CBranchNode * proot)
{
vector<CCorpNode*> subordinateList = proot->GetSubordinateInfo();
string info = "";
vector<CCorpNode*>::const_iterator it = subordinateList.begin();
for (; it != subordinateList.end(); it++)
{
if ((*it)->IsLeaf())
{
info.append((*it)->GetInfo());
info.append("\n");
}
else
{
info.append((*it)->GetInfo());
info.append("\n");
info.append(GetTreeInfo(dynamic_cast<CBranchNode*>(*it)));
}
}
return info;
}
static void DoNew()
{
CBranchNode root("赵大", "总经理", 100000);
CBranchNode devDep("钱大", "研发部门经理", 10000);
CBranchNode saleDep("孙大", "销售部门经理", 20000);
CBranchNode financeDep("李大", "财务部门经理", 30000);
CBranchNode firstDevGroup("周三也斜", "开发一组组长", 5000);
CBranchNode secondDevGroup("吴大棒槌", "开发二组组长", 6000);
CLeafNode a("a", "开发人员", 2000);
CLeafNode b("b", "开发人员", 2000);
CLeafNode c("c", "开发人员", 2000);
CLeafNode d("d", "开发人员", 2000);
CLeafNode e("e", "开发人员", 2000);
CLeafNode f("f", "开发人员", 2000);
CLeafNode g("g", "开发人员", 2000);
CLeafNode h("h", "开发人员", 5000);
CLeafNode i("i", "开发人员", 4000);
CLeafNode j("j", "开发人员", 5000);
CLeafNode k("k", "CEO秘书", 8000);
CLeafNode zheng("郑老六", "研发部副经理", 20000);
root.Add(&k);//CEO有三个部门经理和一个秘书
root.Add(&devDep);
root.Add(&saleDep);
root.Add(&financeDep);
devDep.Add(&zheng);//开发部有一个副经理和两个小组
devDep.Add(&firstDevGroup);
devDep.Add(&secondDevGroup);
firstDevGroup.Add(&a);
firstDevGroup.Add(&b);
firstDevGroup.Add(&c);
secondDevGroup.Add(&d);
secondDevGroup.Add(&e);
secondDevGroup.Add(&f);
saleDep.Add(&g);
saleDep.Add(&h);
financeDep.Add(&i);
financeDep.Add(&j);
cout << root.GetInfo().c_str() << endl;
cout << GetTreeInfo(&root).c_str() << endl;
}
int CompositeTest()
{
//使用组合模式后的调用。
DoNew();
return 0;
}