组合模式
组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。
这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。
使用场景
当你发现需求中体现了部分与整体的层次的结构的时候,并且组合对象与单个对象职能相似,统一的使用组合中所有的对象时,可以考虑去使用组合模式来进行设计了。
优缺点
优点:
1、高层模块调用简单。
2、节点自由增加。
缺点:
在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。
注意事项
定义时为具体类。
UML结构图
代码实现
employee.h
创建具体类 - 员工类,把该类当做组合模型添加部门层次结构
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Employee
{
public:
Employee(string name, string department, int salary)
{
this->name = name;
this->department = department;
this->salary = salary;
cout << "Employee name: " + name <<
", department: " + department <<
", salary: " << salary << endl;
}
void add(Employee *employee)
{
vector.push_back(employee);
}
std::vector<Employee*> getVector()
{
return this->vector;
}
string getDetails()
{
string ret = "Employee name: " + name +
", department: " + department +
", salary: " + std::to_string(salary);
return ret;
}
private:
string name;
string department;
int salary;
std::vector<Employee *> vector;
};
main.cpp
实例应用 - 从上级到下级创建不同岗位的员工
级别低的员工归属于级别高的员工,结构层次分明
#include "employee.h"
int main()
{
Employee *CEO = new Employee("Billy", "CEO", 50000);
Employee *vice_president = new Employee("Killy", "vice_president", 40000);
Employee *lead_engineer = new Employee("Ben", "lead_engineer", 40000);
Employee *marketing_manager1 = new Employee("Jason", "marketing_manager1", 30000);
Employee *marketing_manager2 = new Employee("Jon", "marketing_manager2", 30000);
Employee *artisan1 = new Employee("Alice", "artisan1", 10000);
Employee *artisan2 = new Employee("Sam", "artisan2", 10000);
CEO->add(vice_president);
CEO->add(lead_engineer);
vice_president->add(marketing_manager1);
vice_president->add(marketing_manager2);
lead_engineer->add(artisan1);
lead_engineer->add(artisan2);
cout << endl << "CEO: " << CEO->getDetails() << endl;
for (auto employee : CEO->getVector()) {
cout << endl << "Leader: " << employee->getDetails() << endl;
cout << "Employee: " << endl;
for (auto e : employee->getVector()) {
cout << e->getDetails() << endl;
}
}
return 0;
}
运行结果:
Employee name: Billy, department: CEO, salary: 50000
Employee name: Killy, department: vice_president, salary: 40000
Employee name: Ben, department: lead_engineer, salary: 40000
Employee name: Jason, department: marketing_manager1, salary: 30000
Employee name: Jon, department: marketing_manager2, salary: 30000
Employee name: Alice, department: artisan1, salary: 10000
Employee name: Sam, department: artisan2, salary: 10000
CEO: Employee name: Billy, department: CEO, salary: 50000
Leader: Employee name: Killy, department: vice_president, salary: 40000
Employee:
Employee name: Jason, department: marketing_manager1, salary: 30000
Employee name: Jon, department: marketing_manager2, salary: 30000
Leader: Employee name: Ben, department: lead_engineer, salary: 40000
Employee:
Employee name: Alice, department: artisan1, salary: 10000
Employee name: Sam, department: artisan2, salary: 10000
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓