设计模式观后(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; }
相关文章推荐
- 设计模式观后(c++还原之五 单例模式)
- 设计模式观后(c++还原之二十四 解释器模式)
- 设计模式观后(c++还原之七 模板方法模式)
- 设计模式观后(c++还原之十五 策略模式)
- 设计模式观后(c++还原之十九 观察者模式)
- 设计模式C++实现(7)——外观模式、组合模式
- 设计模式观后(c++还原之十六 适配器模式)
- 设计模式观后(c++还原之二十一 备忘录模式)
- 设计模式观后(c++还原 里氏替换原则)
- 设计模式观后(c++还原之十一 中介者模式)
- 设计模式观后(c++还原之十七 迭代器模式)
- 设计模式观后(c++还原之十三 责任链模式)
- 设计模式观后(c++还原之三 接口隔离)
- 设计模式C++实现(7)——外观模式、组合模式
- 设计模式观后(c++还原之六 虚拟工厂)
- 设计模式C++实现(7)——外观模式、组合模式
- 设计模式观后(c++还原之四 开闭原则 )
- 设计模式观后(c++还原之八 建造者模式)
- 设计模式观后(c++还原之九 代理模式)
- 设计模式观后(c++还原之二十二 访问者模式)