什么是组合模式?
在GOF的《设计模式:可复用面向对象软件的基础》一书中对组合模式是这样说的:将对象组合成树形结构以表示“部分-整体”的层次结构。组合(Composite)模式使得用户对单个对象和组合对象的使用具有一致性。
组合模式(Composite)将小对象组合成树形结构,使用户操作组合对象如同操作一个单个对象。组合模式定义了“部分-整体”的层次结构,基本对象可以被组合成更大的对象,而且这种操作是可重复的,不断重复下去就可以得到一个非常大的组合对象,但这些组合对象与基本对象拥有相同的接口,因而组合是透明的,用法完全一致。
我们这样来简单的理解组合模式,组合模式就是把一些现有的对象或者元素,经过组合后组成新的对象,新的对象提供内部方法,可以让我们很方便的完成这些元素或者内部对象的访问和操作。我们也可以把组合对象理解成一个容器,容器提供各种访问其内部对象或者元素的API,我们只需要使用这些方法就可以操作它了。
例子
在UI的设计中,最基本的控件是诸如Button、Edit这样的控件,相当于是这里的Leaf组件,而比较复杂的控件比如Panel则可也看做是由这些基本的组件组合起来的控件,相当于这里的Composite,它们之间有一些行为含义是相同的,比如在控件上作一个点击,移动操作等等的,这些都可以定义为抽象基类中的接口虚函数,由各个派生类去实现之,这些都会有的行为就是这里的Operation函数,而添加、删除等进行组件组合的操作只有非叶子结点才可能有,所以虚拟基类中只是提供接口而且默认的实现是什么都不做。
透明模式和安全模式:
- 透明模式,是指子类中的所有对象都有着和基类相同的访问接口, 缺点是, 对一些叶节点而言有些接口是没有意义的。
- 安全模式, 是指子类中叶节点和非叶节点有着不同的接口, 客户端在调用的时候需要做相应判断, 但是比较安全
CODE
/******************************/
/* Component */
/* | */
/* --------------- */
/* Leaf*N Composite */
/* | */
/* Leaf*N */
/******************************/
#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
//公司基类
class CCompany {
protected:
string name;
public:
CCompany(string _name) :name(_name) {};
string getName() { return name; }
virtual void Add(CCompany* c) = 0;
virtual void Remove(CCompany* c) = 0;
virtual void Display(int depth = 1) = 0;
virtual void Duty(int depth = 1) = 0;
};
//实体公司类型(总公司、分公司、子公司)
class CConcreteCompany :public CCompany {
private:
vector<CCompany*>child;
public:
CConcreteCompany(string _name) :CCompany(_name) {}
void Add(CCompany* c) { child.push_back(c); }
void Remove(CCompany* c) {
for (int i = 0; i < child.size(); i++)
if (c->getName() == child[i]->getName()) {
child.erase(child.begin() + i);
cout << "关闭" << c->getName();
break;
}
}
void Display(int depth) {
cout << string(depth, '-') << name << endl;
for (int i = 0; i < child.size(); i++) {
child[i]->Display(depth + 2);
}
}
void Duty(int depth) {
cout << string(depth, ' ') << name << endl;
for (int i = 0; i < child.size(); i++) {
child[i]->Duty(depth + 2);
}
}
};
//人力部
class HRDepartment:public CCompany {
public:
HRDepartment(string _name) :CCompany(_name) {}
void Add(CCompany* c) {}
void Remove(CCompany* c) {}
void Display(int depth) {
cout << string(depth, '-') << name << endl;
}
void Duty(int depth) {
cout << string(depth, ' ') << left << setw(20) << name << "\t员工招聘管理" << endl;
}
};
//财务部
class FinanceDepartment :public CCompany {
public:
FinanceDepartment(string _name) :CCompany(_name) {}
void Add(CCompany* c) {}
void Remove(CCompany* c) {}
void Display(int depth) {
cout << string(depth, '-') << name << endl;
}
void Duty(int depth) {
cout << string(depth, ' ') << left << setw(20) << name << "\t公司财务收支管理" << endl;
}
};
//销售部
class SaleDepartment :public CCompany {
public:
SaleDepartment(string _name) :CCompany(_name) {}
void Add(CCompany* c) {}
void Remove(CCompany* c) {}
void Display(int depth) {
cout << string(depth, '-') << name << endl;
}
void Duty(int depth) {
cout << string(depth, ' ') << left << setw(20) << name << "\t公司产品营销管理" << endl;
}
};
int main(void) {
CConcreteCompany* root = new CConcreteCompany("江西总公司");
CConcreteCompany* jl = new CConcreteCompany("吉林分公司");
CConcreteCompany* jlson = new CConcreteCompany("吉林分公司子公司");
HRDepartment* jlson_hr = new HRDepartment("吉林分公司子公司人力部");
FinanceDepartment* jlson_f = new FinanceDepartment("吉林分公司子公司财务部");
SaleDepartment* jlson_sale = new SaleDepartment("吉林分公司子公司销售部");
jlson->Add(jlson_hr);
jlson->Add(jlson_f);
jlson->Add(jlson_sale);
jl->Add(jlson);
HRDepartment* jl_hr = new HRDepartment("吉林分公司人力部");
FinanceDepartment* jl_f = new FinanceDepartment("吉林分公司财务部");
SaleDepartment* jl_sale = new SaleDepartment("吉林分公司销售部");
jl->Add(jl_hr);
jl->Add(jl_f);
jl->Add(jl_sale);
root->Add(jl);
CConcreteCompany* beij = new CConcreteCompany("北京办事处");
HRDepartment* beij_hr = new HRDepartment("北京办事处人力部");
FinanceDepartment* beij_f = new FinanceDepartment("北京办事处财务部");
SaleDepartment* beij_sale = new SaleDepartment("北京办事处销售部");
beij->Add(beij_hr);
beij->Add(beij_f);
beij->Add(beij_sale);
root->Add(beij);
HRDepartment* root_hr = new HRDepartment("总公司人力资源");
FinanceDepartment* root_f = new FinanceDepartment("总公司财务部");
SaleDepartment* root_sale = new SaleDepartment("总公司销售部");
root->Add(root_hr);
root->Add(root_f);
root->Add(root_sale);
cout << "公司组织结构" << endl;
root->Display(1);
cout << endl;
cout << "各部门职责" << endl;
root->Duty(1);
cout << endl;
return 0;
}
参考博文: