設計模式—組合模式(十五)

        軟件領域中的設計模式的重要性不言而喻。設計模式中運用了面向對象編程語言的重要特性:封裝、繼承、多態。雖然知道這些特性的定義但是並沒有做到真正的理解,這樣特性有什麼作用?用於什麼場合中等等問題,帶着疑問開始學習設計模式,主要參考《大話設計模式》和《設計模式:可複用面向對象軟件的基礎》兩本書。

        組合模式(Composite):將對象組合成樹形結構以表示‘部分—整體’的層次結構。組合模式使得用戶對單個對象和組合對象的使用具有一致性;

        例如:整體和部分可以被一致對待(如WORD中複製一個文字、一段文字、一篇文章都是一樣的操作)


#include <iostream>
#include <string>
#include <vector>
using namespace std;
//組合抽象類
class Component
{
public:
	string m_strName;
	Component(string strName)
	{
		m_strName = strName;
	}
	virtual void Add(Component* com) = 0;
	virtual void Display(int nDepth) = 0;
};
//葉子類——沒分支
class Leaf : public Component
{
public:
	Leaf(string strName) : Component(strName){}

	virtual void Add(Component* com)
	{
		cout << "leaf can't add" << endl;
	}

	virtual void Display(int nDepth)
	{
		string strtemp;
		for (int i = 0; i < nDepth; i++)
		{
			strtemp += "-";
		}
		strtemp += m_strName;
		cout << strtemp << endl;
	}
};
//組合類——有分支
class Composite : public Component
{
private:
	vector<Component*> m_component;
public:
	Composite(string strName) : Component(strName){}

	virtual void Add(Component* com)
	{
		m_component.push_back(com);
	}

	virtual void Display(int nDepth)
	{
		string strtemp;
		for (int i = 0; i < nDepth; i++)
		{
			strtemp += "-";
		}
		strtemp += m_strName;
		cout << strtemp << endl;
		for (Component* p:m_component)
		{
			p->Display(nDepth + 2);
		}
	}

};

//客戶端
int main()
{
	Composite* p = new Composite("上海總部");
	p->Add(new Composite("合肥分公司"));
	p->Add(new Composite("南京分公司"));

	Composite* p1 = new Composite("上海財務總部");
	p1->Add(new Leaf("合肥財務部"));
	p1->Add(new Leaf("南京財務部"));

	p->Add(p1);
	p->Display(1);
	return 0;
}

例:將下圖框架用組合模式實現



#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Company
{
protected:
	string m_strName;
public:
	Company(string strName)
	{
		m_strName = strName;
	}

	virtual void Add(Company* c) = 0;
	virtual void Display(int nDepth) = 0;
	virtual void LineOfDuty() = 0;
};

class ConcreteCompany : public Company
{
private:
	vector<Company*> m_company;
public:
	ConcreteCompany(string strName) :Company(strName){}

	virtual void Add(Company* c)
	{
		m_company.push_back(c);
	}
	virtual void Display(int nDepth)
	{
		string strtemp;
		for (int i = 0; i < nDepth; i++)
		{
			strtemp += "-";
		}
		strtemp += m_strName;
		cout << strtemp << endl;

		vector<Company*>::iterator p = m_company.begin();
		while (p != m_company.end())
		{
			(*p)->Display(nDepth + 2);
			p++;
		}
	}
	virtual void LineOfDuty()
	{
		vector<Company*>::iterator p = m_company.begin();
		while (p != m_company.end())
		{
			(*p)->LineOfDuty();
			p++;
		}
	}
};

class HrDepartment : public Company
{
public:

	HrDepartment(string strname) : Company(strname){}

	virtual void Display(int nDepth)
	{
		string strtemp;
		for (int i = 0; i < nDepth; i++)
		{
			strtemp += "-";
		}

		strtemp += m_strName;
		cout << strtemp << endl;
	}
	virtual void Add(Company* c)
	{}

	virtual void LineOfDuty()
	{
		cout << m_strName << ":招聘人才" << endl;
	}
};

class FinanceDepartment : public Company
{
public:

	FinanceDepartment(string strname) : Company(strname){}

	virtual void Display(int nDepth)
	{
		string strtemp;
		for (int i = 0; i < nDepth; i++)
		{
			strtemp += "-";
		}

		strtemp += m_strName;
		cout << strtemp << endl;
	}
	virtual void Add(Company* c)
	{}

	virtual void LineOfDuty()
	{
		cout << m_strName << ":管理財務" << endl;
	}
};

//客戶端:
int main()
{
	ConcreteCompany *p = new ConcreteCompany("北京總公司");
	p->Add(new HrDepartment("總公司人力資源部"));
	p->Add(new FinanceDepartment("總公司財務部"));

	ConcreteCompany *p1 = new ConcreteCompany("上海華東分公司");
	p1->Add(new HrDepartment("華東人力資源部"));
	p1->Add(new FinanceDepartment("華東分公司財務部"));
	p->Add(p1);

	ConcreteCompany *p2 = new ConcreteCompany("南京辦事處");
	p2->Add(new HrDepartment("南京辦事處人力資源部"));
	p2->Add(new FinanceDepartment("南京辦事處財務部"));
	p1->Add(p2);

	ConcreteCompany *p3 = new ConcreteCompany("杭州辦事處");
	p3->Add(new HrDepartment("杭州辦事處人力資源部"));
	p3->Add(new FinanceDepartment("杭州辦事處財務部"));
	p1->Add(p3);
	cout << "結構圖:" << endl;
	p->Display(1);
	cout << "職責:" << endl;
	p->LineOfDuty();
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章