您的位置:首页 > 编程语言 > C语言/C++

设计模式观后(c++还原之十八 组合模式)

2014-09-25 21:33 369 查看
//组合模式
//作者这个模式举得例子很有趣:树、根、叶;
//用上面的思想要把员工管理组合在一起。
//经理添加员工,获取信息。组长添加员工,获取信息等,树结构。
//抽象员工接口、(根节点)
class ICorp {
public:
virtual string GetInfo() = 0;
virtual int GetType() {return 0;}//获取职位类型0:树叶 1:树枝
virtual string IntToChar(int a) {
char buffer[10] = {0};
_itoa_s(a, buffer, 10, 10);
string temp(buffer);
return temp;
}
};
//树叶接口(组长接口)
class Leaf : public ICorp {
private:
string m_strName;
string m_strPosition;//职位
int m_nSalary;
public:
Leaf(string name, string position, int salary) {
m_strName = name;
m_strPosition = position;
m_nSalary = salary;
}
string GetInfo() {
string info;
info += "姓名:" +m_strName;
info += "\n职位:" + m_strPosition;
info += "\n薪水" + IntToChar(m_nSalary);
info += "\n";
return info;
}
};
//树枝接口(抽象经理类)
class IBranch : public ICorp {
public:
//添加成员(小兵或经理)
virtual void AddSubordinate(ICorp* corp) = 0;
virtual int GetType() {return 1;}
//获取下属信息
virtual deque<ICorp*>* GetSubordinate() = 0;
};
//树枝实现
class Branch : public IBranch {
private:
string m_strName;
string m_strPosition;//职位
int m_nSalary;
deque<ICorp*>* m_pDICorp;
public:
Branch(string name, string positon, int salary)
: m_pDICorp(new deque<ICorp*>) {
m_strName = name;
m_strPosition = positon;
m_nSalary = salary;
}
void AddSubordinate(ICorp* corp) {
m_pDICorp->push_back(corp);
}
deque<ICorp*>* GetSubordinate() {
return m_pDICorp;
}
string GetInfo() {
string info;
info += "姓名:" +m_strName;
info += "\n职位:" + m_strPosition;
info += "\n薪水" + IntToChar(m_nSalary);
info += "\n";
return info;
}
};

class Client {
public:
static Branch* CompositeCorpTree() {
//先产生根,总经理
Branch* root = new Branch("王大", "总经理", 100000);
//产生部门经理
Branch* develop = new Branch("刘瘸", "研发部经理", 1000);
Branch* sales	= new Branch("马二", "销售经理", 2000);
Branch* finance = new Branch("赵三", "财务经理", 3000);
//产生三个组长
Branch* first	= new Branch("杨三", "开发一组", 5000);
Branch* second	= new Branch("吴大", "开发二组", 6000);
//产生所有小兵
Leaf* a = new Leaf("a", "码农", 2000);
Leaf* b = new Leaf("b", "码农", 2000);
Leaf* c = new Leaf("c", "秘书", 2000);
Leaf* d = new Leaf("d", "财务", 2000);
Leaf* e = new Leaf("e", "销售", 2000);

//开始组装
root->AddSubordinate(c);
root->AddSubordinate(develop);
root->AddSubordinate(sales);
root->AddSubordinate(finance);
//研发经理
develop->AddSubordinate(first);
develop->AddSubordinate(second);
//小组成员
first->AddSubordinate(a);
second->AddSubordinate(b);
//看销售
sales->AddSubordinate(e);
//看财务
finance->AddSubordinate(d);

return root;
}
static string GetTreeInfo(Branch* root) {
deque<ICorp*>* p_ICorp = root->GetSubordinate();
string info;
for (deque<ICorp*>::iterator i = p_ICorp->begin();
i != p_ICorp->end(); i++)
{
if ((*i)->GetType() == 1) {
string temp;
temp = (*i)->GetInfo() + "\n";
info += temp + GetTreeInfo((Branch*)(*i));
} else {
info += (*i)->GetInfo() + "\n";
}
}
return info;
}

static void main() {
Branch* ceo = CompositeCorpTree();
cout << ceo->GetInfo();
cout << GetTreeInfo(ceo);
}
};
//这个设计用递归应该是最好的方法了吧!
//递归遍历
//组合模式在于高层模块调用简单
//节点自由添加
//开始有点像stl的迭代器设计了
int _tmain(int argc, _TCHAR* argv[])
{
Client::main();
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: