设计模式之组合模式

组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构。使得用户对单个对象和组合对象的使用具有一致性。
适用性:想表示对象的部分-整体层次结构,希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
典型表示结构图:


Component:
声明组合中的对象声明接口;在适当的情况下,实现所有类共有接口的缺省行为;声明一个接口用于访问和管理Component的子组件;(可选)在递归结构中定义一个接口,用于访问一个父部件,并在适当情况下实现它。
Leaf:
在组合中表示叶节点对象,叶节点没有子节点;在组合中定义图元对象的行为。
Composite:
定义有子部件的那些部件的行为;存储子部件;在Component接口操纵组合部件的对象。
Client:
通过Component接口操纵组合部件对象。

协作:
用户使用Component类接口与组合结构中的对象进行交互。如果接收者是一个叶节点,则直接处理请求。如果接收者是Composite,它通常将请求发送给它的子部件,在转发请求之前与之后可能执行一些辅助操作。

效果

定义了包含基本对象和组合对象的类层次结构;简化客户代码;使得更容易增加新类型的组件;设计更一般化.

代码(代码木有考虑内存泄露,,木有释放内存- -吐舌头,先这样吧):

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

class Company
{
public:
	Company(string name) 
	{
		_name = name;
	}
	string Name() const
	{
		return _name;
	}
	virtual void Add(Company* c) = 0;
	virtual void Remove(Company* c) = 0;
	virtual void Display(int depth) const = 0 ;
	virtual void LineOfDuty() const = 0;
private:
	string _name;
};

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

	}

	void Add(Company* c)
	{
		children.push_back(c);
	}

	void Remove(Company* c)
	{
		vector<Company*>::iterator pos;
		//remove:返回新区间的逻辑终点。并没有实际删除元素
		pos = remove(children.begin(), children.end(), c);
		//erase删除逻辑终点与实际终点的元素
		children.erase(pos, children.end());
	}

	 void Display(int depth) const
	 {
		 string ss(depth, '-');
		 cout << ss << Name() << endl;
		 /* for (vector<Company*>::const_iterator it = children.begin();
		 it != children.end(); it++)
		 {
		 (*it)->Display(depth+2);
		 } */
		 for_each(children.begin(), children.end(), bind2nd(mem_fun(&Company::Display), depth+2));
	 }

	 void LineOfDuty() const
	 {
	/*	 for (vector<Company*>::const_iterator it = children.begin();
			 it != children.end(); it++)
		 {
			 (*it)->LineOfDuty();
		 } */
		 for_each(children.begin(), children.end(), mem_fun(&Company::LineOfDuty));
	 }
protected:
private:
	vector<Company*> children;
};

//人力资源部
class HRDeaprtment : public Company
{
public:
	HRDeaprtment(string name) : Company(name)
	{

	}

	void Add(Company* c)
	{

	}

	void Remove(Company* c)
	{

	}

	void Display(int depth) const
	{
		string ss(depth, '-');
		cout << ss << Name() << endl;
	}

	void LineOfDuty() const
	{
		cout << Name() << " 员工招聘培训管理" << endl;
	}
};

//财务部
class FinanceDeaprtment : public Company
{
public:
	FinanceDeaprtment(string name) : Company(name)
	{

	}

	void Add(Company* c)
	{

	}

	void Remove(Company* c)
	{

	}

	void Display(int depth) const
	{
		string ss(depth, '-');
		cout << ss << Name() << endl;
	}

	void LineOfDuty() const
	{
		cout << Name() << " 公司财务收支管理" << endl;
	}
};
#include "composite.h"

int main()
{
	ConcreteCompany *root = new ConcreteCompany("北京总公司");
	root->Add(new HRDeaprtment("总公司人力资源部"));
	root->Add(new FinanceDeaprtment("总公司财务部"));

	ConcreteCompany *comp = new ConcreteCompany("上海华东分公司");
	comp->Add(new HRDeaprtment("上海华东分公司人力资源部"));
	comp->Add(new FinanceDeaprtment("上海华东分公司财务部"));
	root->Add(comp);

	ConcreteCompany *comp1 = new ConcreteCompany("南京办事处");
	comp1->Add(new HRDeaprtment("南京办事处人力资源部"));
	comp1->Add(new FinanceDeaprtment("南京办事处公司财务部"));
	root->Add(comp1);

	ConcreteCompany *comp2 = new ConcreteCompany("杭州办事处");
	comp2->Add(new HRDeaprtment("杭州办事处人力资源部"));
	comp2->Add(new FinanceDeaprtment("杭州办事处财务部"));
	root->Add(comp2);

	cout << "结构图:\n";
	root->Display(1);
	cout << "职责:\n";
	root->LineOfDuty();

	root->Remove(comp);
	cout << "\n结构图:\n";
	root->Display(1);
	cout << "职责:\n";
	root->LineOfDuty();

	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值