c++设计模式----组合模式(Composite)

 什么是组合模式呢?

                               

DP书上给出的定义:将对象组合成树形结构以表示“部分-整体”的层次结构。组合使得用户对单个对象和组合对象的使用具有一致性。注意两个字“树形”。这种树形结构在现实生活中随处可见,比如一个集团公司,它有一个母公司,下设很多家子公司。不管是母公司还是子公司,都有各自直属的财务部、人力资源部、销售部等。对于母公司来说,不论是子公司,还是直属的财务部、人力资源部,都是它的部门。整个公司的部门拓扑图就是一个树形结构。

                                                                clip_image008

意图:

将对象组合成树形结构以表示“部分-整体”的层次结构。composite使得用户对单个对象和组合对象的使用具有一致性。 
适用性:

你想表示对象的部分-整体层次结构。

你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

实现如下:

#include 
#include 
#include 
#include 
using namespace std;

class Company
{
public:
	Company(string name) :m_name(name){}
	virtual void add(Company *p) = 0;
	virtual void remove(Company *p) = 0;
	virtual void Print() = 0;
	string GetName()
	{
		return m_name;
	}


	virtual ~Company(){}

private:
	string m_name;
};

class ConcreteCompany :public Company
{
public:
	ConcreteCompany(string name) :Company(name){};

	void add(Company *p)
	{
		p_company.push_back(p);
	}

	void remove(Company *p)
	{
		for (vector::iterator iter = p_company.begin(); iter != p_company.end(); ++iter)
		{
			if ((*iter)->GetName() == p->GetName())
			{
				if (*iter != NULL)
				{
					cout << "----delete1111111111111111111 " << (*iter)->GetName() << "----" << endl;
					delete *iter;
					*iter = NULL;
				}
				p_company.erase(iter);
				break;
			}
		}
	}

	~ConcreteCompany()
	{
		vector::iterator iter = p_company.begin();
		while (iter != p_company.end())
		{
			if (*iter != NULL)
			{
				cout << "----delete " << (*iter)->GetName() << "----" << endl;
				delete *iter;
				*iter = NULL;
			}
			p_company.erase(iter);
			iter = p_company.begin();
		}
	}

	virtual void Print()
	{
		for (vector::iterator it = p_company.begin(); it != p_company.end(); ++it)
		{
			cout << (*it)->GetName() << endl;
		}
	}
private:
	vector  p_company;
};

class Leaf : public Company
{
public:
	Leaf(string name) : Company(name)
	{}
	void Operation()
	{
		cout << "I'm " << GetName() << endl;
	}
	void add(Company *p){}
	void remove(Company *p){}
	void Print(){}
};

int main(int argc, char *argv[])
{
	Company *pNode = new ConcreteCompany("Beijing Head Office");
	Company *pNodeHr = new Leaf("Beijing Human Resources Department");
	Company *pSubNodeSh = new ConcreteCompany("Shanghai Branch");
	Company *pSubNodeCd = new ConcreteCompany("Chengdu Branch");
	Company *pSubNodeBt = new ConcreteCompany("Baotou Branch");
	pNode->add(pNodeHr);
	pNode->add(pSubNodeSh);
	pNode->add(pSubNodeCd);
	pNode->add(pSubNodeBt);
	pNode->Print();

	Company *pSubNodeShHr = new Leaf("Shanghai Human Resources Department");
	Company *pSubNodeShCg = new Leaf("Shanghai Purchasing Department");
	Company *pSubNodeShXs = new Leaf("Shanghai Sales department");
	Company *pSubNodeShZb = new Leaf("Shanghai Quality supervision Department");
	pSubNodeSh->add(pSubNodeShHr);
	pSubNodeSh->add(pSubNodeShCg);
	pSubNodeSh->add(pSubNodeShXs);
	pSubNodeSh->add(pSubNodeShZb);
	pNode->Print();

	// 公司不景气,需要关闭上海质量监督部门
	pSubNodeSh->remove(pSubNodeShZb);
	if (pNode != NULL)
	{
		delete pNode;
		pNode = NULL;
	}
	return 0;
}

使用智能指针管理上面的代码中指针:

             



#include 
#include 
#include 
#include 
using namespace std;

class Company
{
public:
	Company(string name) :m_name(name){}
	virtual void add(shared_ptr p) = 0;
	virtual void remove(shared_ptr p) = 0;
	string GetName()
	{
		return m_name;
	}

	virtual void Show(int denpth) {}

	virtual ~Company(){}

private:
	string m_name;
};

class ConcreteCompany :public Company
{
public:
	ConcreteCompany(string name) :Company(name){};

	void add(shared_ptr p)
	{
		p_company.push_back(p);
	}

	void remove(shared_ptr p)
	{
		for (vector>::iterator iter = p_company.begin(); iter != p_company.end(); ++iter)
		{
			if ((*iter)->GetName() == p->GetName())
			{
				p_company.erase(iter);
			}
		}
	}

	void Show(int denpth)
	{
		for (int i = 0; i < denpth; i++)
			cout << "-";
		cout << GetName() << endl;
		for (vector>::iterator it = p_company.begin(); it != p_company.end(); ++it)
		{
			(*it)->Show(denpth+2);
		}
	}

	~ConcreteCompany(){}

private:
	vector>  p_company;
};


class Leaf_F : public Company
{
public:
	Leaf_F(string name) : Company(name){}

	void add(shared_ptr p){}
	void remove(shared_ptr p){}
	shared_ptr GetChild(int index)
	{
		return NULL;
	}


	void Show(int denpth)
	{
		for (int i = 0; i < denpth; i++)
			cout << "-";
		cout << GetName() << endl;
	}

	~Leaf_F(){}
};

class Leaf_M : public Company
{
public:
	Leaf_M(string name) : Company(name){}

	void add(shared_ptr p){}
	void remove(shared_ptr p){}
	shared_ptr GetChild(int index)
	{
		return NULL;
	}

	void Show(int denpth)
	{
		for (int i = 0; i < denpth; i++)
			cout << "-";
		cout << GetName() << endl;
	}

	~Leaf_M(){}
};

int main()
{
	//总公司
	shared_ptr root(new ConcreteCompany("总公司"));
	shared_ptr leaf1(new Leaf_F("财务部"));
	shared_ptr leaf2(new Leaf_M("人力资源部"));
	root->add(leaf1);
	root->add(leaf2);

	//分公司A  
	shared_ptr mid1 = make_shared(ConcreteCompany("分公司A"));
	shared_ptr leaf3 = make_shared(Leaf_F("财务部"));
	shared_ptr leaf4 = make_shared(Leaf_M("人力资源部"));
	mid1->add(leaf3);
	mid1->add(leaf4);
	root->add(mid1);

	//分公司B  
	shared_ptr mid2 = make_shared(ConcreteCompany("分公司B"));
	shared_ptr leaf5 = make_shared(Leaf_F("财务部"));
	shared_ptr leaf6 = make_shared(Leaf_M("人力资源部"));
	mid2->add(leaf5);
	mid2->add(leaf6);
	root->add(mid2);


	root->Show(0);
}



你可能感兴趣的:(c++设计模式)