设计模式8 - 组合模式

作者:billy
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处

组合模式

组合模式(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
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值