设计模式观后(c++还原之十八 组合模式)
来源:互联网 发布:王者荣耀挂机软件 编辑:程序博客网 时间:2024/06/05 11:03
//组合模式//作者这个模式举得例子很有趣:树、根、叶;//用上面的思想要把员工管理组合在一起。//经理添加员工,获取信息。组长添加员工,获取信息等,树结构。//抽象员工接口、(根节点)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;}
0 0
- 设计模式观后(c++还原之十八 组合模式)
- 十八、设计模式 之 组合模式
- 十八、组合设计模式
- Android设计模式(十八)-组合模式
- 设计模式(c++)笔记之十八(Mediator模式)
- 设计模式观后(c++还原之五 单例模式)
- 设计模式观后(c++还原之六 工厂模式)
- 设计模式观后(c++还原之七 模板方法模式)
- 设计模式观后(c++还原之八 建造者模式)
- 设计模式观后(c++还原之九 代理模式)
- 设计模式观后(c++还原之十 代理模式)
- 设计模式观后(c++还原之十一 中介者模式)
- 设计模式观后(c++还原之十二 命令模式)
- 设计模式观后(c++还原之十三 责任链模式)
- 设计模式观后(c++还原之十四 修饰模式)
- 设计模式观后(c++还原之十五 策略模式)
- 设计模式观后(c++还原之十六 适配器模式)
- 设计模式观后(c++还原之十七 迭代器模式)
- MySQL集群简介与配置详解
- 记录下Android两个小细节
- GNU ARM汇编(综合的学习资料)
- Duilib学习笔记《01》— duilib整体框架认识
- android布局
- 设计模式观后(c++还原之十八 组合模式)
- 递归求解整数划分问题
- linux 网络编程之TIME_WAIT状态
- java--Java线程池
- svn 命令操作
- 关于文件的读写操作以及文件的打开和保存
- 清除元素浮动带来的影响
- 自定义控件
- openVswitch(OVS)源代码的分析技巧(哈希桶结构体为例)