大话设计模式C++实现-第19章-组合模式

一、UML图


关键词:Leaf是叶子,Composite是非叶子节点,Composite包括Leaf。


二、概念

组合模式(Composite):将对象组合成树形结构以表示“部分-总体”的层次结构。

组合模式使得用户对单个对象和组合对象的使用具有一致性。


三、说明

角色:

(1)Component:为组合中的对象声明接口。在适当情况下,实现全部类共同拥有接口的默认行为。声明一个接口用于訪问和管理Component 的子部件。

(2)Leaf:在组合中白哦是叶节点对象,叶节点没有子节点。

(3)Composite:定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,比方添加Add和删除Remove。

什么时候使用组合模式?

当你发现需求中是体现部分与总体层次的结构时。以及你希望用户能够忽略组合对象与单个对象的不同,统一地使用组合结构中的全部对象时,就应该考虑用组合模式了。

使用组合模式的优点?

(1)组合模式定义了包括基本对象(Leaf)和组合对象(Composite)的类层次结构。

基本对象能够被组合成更复杂的组合对象,而这个组合对象又能够被组合,这样不断地地柜下去,客户代码中。不论什么用到基本对象的地方都能够使用组合对象了。

(2)用户不用关心究竟是处理一个叶子节点还是处理一个组合组件,也就不用为定义组合而写一些推断语句了。

(3)简而言之,组合模式让客户能够一致的使用组合结构和单个对象。


四、C++实现

(1)Composite.h

#ifndef COMPOSITE_H
#define COMPOSITE_H

#include <iostream>
#include <list>
#include <string>

//Component:此处为抽象公司类
class Company
{
protected:
	std::string name;
public:
	Company(std::string name)
	{
		this->name=name;
	}

	//添加节点
	virtual void Add(Company* c)=0;
	//删除节点
	virtual void Remove(Company* c)=0;
	//展示
	virtual void Display(int depth)=0;
	//职责
	virtual void LineOfDuty()=0;

	//运算符重载
	inline bool operator==(const Company &company) const
	{
		return this->name==company.name;
	}
};

//Composite:详细公司类
class ConcreteCompany:public Company
{
private:
	std::list<Company*> *children;
public:
	ConcreteCompany(std::string name):Company(name)
	{
		children=new std::list<Company*>;
	}
	~ConcreteCompany()
	{
		for(std::list<Company*>::iterator it=children->begin();it!=children->end();it++)
			delete *it;
		delete children;
	}
	//添加叶子节点
	void Add(Company* c)
	{
		children->push_back(c);
	}
	//删除叶子节点
	void Remove(Company* c)
	{
		for(std::list<Company*>::iterator it=children->begin();it!=children->end();it++)
		{
			if(**it==*c)
			{
				children->erase(it);
				break;
			}
		}
	}
	//打印
	void Display(int depth)
	{
		for(int i=0;i<depth;i++)
			std::cout<<"-";
		std::cout<<name<<std::endl;

		for(std::list<Company*>::iterator it=children->begin();it!=children->end();it++)
			(*it)->Display(depth+4);
	}
	//职责
	void LineOfDuty()
	{
		for(std::list<Company*>::iterator it=children->begin();it!=children->end();it++)
			(*it)->LineOfDuty();
	}
};

//Leaf:人力资源部
class HRDepartment:public Company
{
public:
	HRDepartment(std::string name):Company(name){}

	void Add(Company* c){}
	void Remove(Company* c){}
	void Display(int depth)
	{
		for(int i=0;i<depth;i++)
			std::cout<<"-";
		std::cout<<name<<std::endl;
	}
	void LineOfDuty()
	{
		std::cout<<name<<"  员工招聘培训管理"<<std::endl;
	}
};

//Leaf:財务部
class FinanceDepartment:public Company
{
public:
	FinanceDepartment(std::string name):Company(name){}

	void Add(Company* c){}
	void Remove(Company* c){}
	void Display(int depth)
	{
		for(int i=0;i<depth;i++)
			std::cout<<"-";
		std::cout<<name<<std::endl;
	}
	void LineOfDuty()
	{
		std::cout<<name<<"  公司財务收支管理"<<std::endl;
	}
};

#endif


(2)Client.cpp

#include "Composite.h"
#include <iostream>
#include <string>
#include <cstdlib>

//Client,client
void main()
{
	Company* root=new ConcreteCompany("北京总公司");
	root->Add(new HRDepartment("总公司人力资源部"));
	root->Add(new FinanceDepartment("总公司財务处"));

	Company* comp=new ConcreteCompany("上海华东分公司");
	comp->Add(new HRDepartment("华东分公司人力资源部"));
	comp->Add(new FinanceDepartment("华东分公司財务处"));

	root->Add(comp);

	Company* comp1=new ConcreteCompany("南京办事处");
	comp1->Add(new HRDepartment("南京办事处人力资源部"));
	comp1->Add(new FinanceDepartment("南京办事处財务处"));

	comp->Add(comp1);

	Company* comp2=new ConcreteCompany("杭州办事处");
	comp2->Add(new HRDepartment("杭州办事处人力资源部"));
	comp2->Add(new FinanceDepartment("杭州办事处財务处"));

	comp->Add(comp2);

	std::cout<<"结构图:"<<std::endl<<std::endl;
	root->Display(1);

	std::cout<<std::endl<<"职责:"<<std::endl<<std::endl;
	root->LineOfDuty();

	delete root;
	system("pause");
}


(3)执行截图




原文地址:https://www.cnblogs.com/liguangsunls/p/6822380.html