设计模式C++描述----11.组合(Composite)模式
上一篇:http://www.zzzyk.com/kf/201205/132469.html
一. 举例
这个例子是书上的,假设有一个公司的组结结构如下:
它的结构很像一棵树,其中人力资源部和财务部是没有子结点的,具体公司才有子结点。
而且最关健的是,它的每一层结构很相似。
代码实现如下:
[cpp] view plaincopyprint?//公司类,提供接口
class Company
{
public:
Company(string name)
{
m_name = name;
}
virtual ~Company()
{}
virtual void Add(Company *pCom)
{}
virtual void Display(int depth)
{}
protected:
string m_name;
};
//具体公司
class ConcreteCompany : public Company
{
public:
ConcreteCompany(string name): Company(name)
{}
virtual ~ConcreteCompany()
{}
//增加子树或叶子
void Add(Company *pCom)
{
m_listCompany.push_back(pCom);
}
//显示
void Display(int depth)
{
for(int i = 0;i < depth; i++)
{
cout<<"-";
}
cout<< m_name << endl;
list<Company *>::iterator iter = m_listCompany.begin();
for(; iter != m_listCompany.end(); iter++) //显示下层结点
{
(*iter)->Display(depth + 2);
}
}
private:
list<Company *> m_listCompany;
};
//具体的部门,财务部
class FinanceDepartment : public Company
{
public:
FinanceDepartment(string name):Company(name)
{}
virtual ~FinanceDepartment()
{}
//只需显示,无限添加函数,因为已是叶结点
virtual void Display(int depth)
{
for(int i = 0; i < depth; i++)
cout<<"-";
cout<< m_name << endl;
}
};
//具体的部门,人力资源部
class HRDepartment :public Company
{
public:
HRDepartment(string name):Company(name)
{}
virtual ~HRDepartment()
{}
//只需显示,无限添加函数,因为已是叶结点
virtual void Display(int depth)
{
for(int i = 0; i < depth; i++)
{
cout<<"-";
}
cout<< m_name << endl;
}
};
//////////////////////////////////////////////////////////////////////////
//测试代码
int main()
{
Company *root = new ConcreteCompany("总公司");
Company *leaf1=new FinanceDepartment("财务部");
Company *leaf2=new HRDepartment("人力资源部");
root->Add(leaf1);
root->Add(leaf2);
//华东分公司
Company *mid1 = new ConcreteCompany("华东分公司");
Company *leaf3=new FinanceDepartment("华东分公司财务部");
Company *leaf4=new HRDepartment("华东分公司人力资源部");
mid1->Add(leaf3);
mid1->Add(leaf4);
root->Add(mid1);
//南京办事处
Company *mid2=new ConcreteCompany("南京办事处");
FinanceDepartment *leaf5=new FinanceDepartment("南京办事处财务部");
HRDepartment *leaf6=new HRDepartment("南京办事处人力资源部");
mid2->Add(leaf5);
mid2->Add(leaf6);
root->Add(mid2);
//杭州办事处
Company *mid3=new ConcreteCompany("杭州办事处");
FinanceDepartment *leaf7=new FinanceDepartment("杭州办事处财务部");
HRDepartment *leaf8=new HRDepartment("杭州办事处人力资源部");
mid3->Add(leaf7);
mid3->Add(leaf8);&nbs
补充:软件开发 , C++ ,