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

//组合模式
//作者这个模式举得例子很有趣:树、根、叶;
//用上面的思想要把员工管理组合在一起。
//经理添加员工,获取信息。组长添加员工,获取信息等,树结构。
//抽象员工接口、(根节点)
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值