组合模式

《大话设计模式》

组合模式:

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

//component.h

#ifndef _COMPONENT_H_
#define _COMPONENT_H_

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

class Company
{
	public:
		Company(const string &str):name(str){}
		const string &getName()
		{
			return name; 
		} 
		virtual ~Company() {}
		virtual void Add(Company *c) = 0;
		virtual void Remove(Company *c) = 0;
		virtual void Display(int depth) = 0;
		virtual void LineOfDuty() = 0;
	protected:
		string name;
};

class ConcreteCompany:public Company
{
	public:
		ConcreteCompany(const string &str):Company(str)	{}
		void Add(Company *c);
		void Remove(Company *c);
		void Display(int depth);
		void LineOfDuty();
	private:
		list<Company *> children;
};

class HRDepartment:public Company
{
	public:
		HRDepartment(const string &str):Company(str){}
		void Add(Company *c){}
		void Remove(Company *c){}
		void Display(int depth);
		void LineOfDuty();
};

class FinanceDepartment:public Company
{
	public:
		FinanceDepartment(const string &str):Company(str){}
		void Add(Company *c){}
		void Remove(Company *c){}
		void Display(int depth);
		void LineOfDuty();
};
#endif

//component.cpp
#include "component.h"

void ConcreteCompany::Add(Company *c)
{
	if(c != NULL)
		children.push_back(c);
}

void ConcreteCompany::Remove(Company *c)
{
	if(c != NULL)
	{
		for(list<Company *>::iterator iter = children.begin(); iter != children.end(); iter++)
		{
			if((*iter)->getName().compare(c->getName()) == 0)
			{
				iter = children.erase(iter);
				iter--;
			}
		}
	}
}

void ConcreteCompany::Display(int depth)
{
	cout << string(depth, '-') << name << endl;
	for(list<Company *>::iterator iter = children.begin(); iter != children.end(); iter++)
		(*iter)->Display(depth + 2);
}

void ConcreteCompany::LineOfDuty()
{
	for(list<Company *>::iterator iter = children.begin(); iter != children.end(); iter++)
		(*iter)->LineOfDuty();
}

void HRDepartment::Display(int depth)
{
	cout << string(depth, '-') << name << endl;
}

void HRDepartment::LineOfDuty()
{
	cout << name << " 员工招聘培训管理。" << endl;
}

void FinanceDepartment::Display(int depth)
{
	cout << string(depth, '-') << name << endl;
}

void FinanceDepartment::LineOfDuty()
{
	cout << name << " 公司财务收支管理。" << endl;
}

//componentMain.cpp

#include "component.h" 

int main()
{
	
	auto_ptr<ConcreteCompany> root(new ConcreteCompany(string("北京总公司")));
	auto_ptr<HRDepartment> hr1(new HRDepartment(string("总公司人力资源部"))); 
	auto_ptr<FinanceDepartment> fi1(new FinanceDepartment(string("总公司财政部"))); 
	root->Add(hr1.get()); 
	root->Add(fi1.get());
	
	auto_ptr<ConcreteCompany> comp(new ConcreteCompany(string("上海华东分公司")));
	auto_ptr<HRDepartment> hr2(new HRDepartment(string("华东分公司人力资源部"))); 
	auto_ptr<FinanceDepartment> fi2(new FinanceDepartment(string("华东分公司财政部")));
	comp->Add(hr2.get()); 
	comp->Add(fi2.get());
	root->Add(comp.get()); 
	
	auto_ptr<ConcreteCompany> comp1(new ConcreteCompany(string("南京办事处")));
	auto_ptr<HRDepartment> hr3(new HRDepartment(string("南京办事处人力资源部"))); 
	auto_ptr<FinanceDepartment> fi3(new FinanceDepartment(string("南京办事处财政部")));
	comp1->Add(hr3.get()); 
	comp1->Add(fi3.get());
	comp->Add(comp1.get());
	
	cout << "结构图:" << endl;
	root->Display(1); 
	cout << "职责:" << endl;
	root->LineOfDuty(); 
	 
	return 0; 
} 

输出结果:

结构图:
-北京总公司
---总公司人力资源部
---总公司财政部
---上海华东分公司
-----华东分公司人力资源部
-----华东分公司财政部
-----南京办事处
-------南京办事处人力资源部
-------南京办事处财政部
职责:
总公司人力资源部 员工招聘培训管理。
总公司财政部 公司财务收支管理。
华东分公司人力资源部 员工招聘培训管理。
华东分公司财政部 公司财务收支管理。
南京办事处人力资源部 员工招聘培训管理。
南京办事处财政部 公司财务收支管理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值