设计模式—组合模式(十五)

        软件领域中的设计模式的重要性不言而喻。设计模式中运用了面向对象编程语言的重要特性:封装、继承、多态。虽然知道这些特性的定义但是并没有做到真正的理解,这样特性有什么作用?用于什么场合中等等问题,带着疑问开始学习设计模式,主要参考《大话设计模式》和《设计模式:可复用面向对象软件的基础》两本书。

        组合模式(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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值