责任链模式(大话设计模式)C/C++版本

责任链模式

C++

cpp 复制代码
#include <iostream>
#include <memory>

using namespace std;
// 请求类
struct Request
{
    std::string requestType;    // 请求类型
    int number;                 // 该请求类型的数量
    std::string requestContent; // 请求内容
};

// 抽象经理类
class Manager
{
private:
    std::string name;
    std::shared_ptr<Manager> superior; // 管理者的上级

public:
    Manager(const std::string &strName) : name(strName) {}

    void SetSuperior(std::shared_ptr<Manager> smartSuperior) // 设置管理者的上级
    {
        superior = smartSuperior;
    }

    std::shared_ptr<Manager> GetSuperior() const
    {
        return superior;
    }

    const std::string &GetName() const
    {
        return name;
    }

    virtual void RequestApplications(const Request &request) = 0;
};

// 经理   经理所能有的权限就是可批准下属两天内的假期
class CommonManager : public Manager
{
public:
    CommonManager(const std::string &strName) : Manager(strName) {}
    virtual void RequestApplications(const Request &request)
    {
        if (request.requestType == "请假" && request.number <= 2)
        {
            std::cout << "" << GetName().c_str() << ":" << request.requestContent.c_str() << " 数量 " << request.number << " 被批准!" << std::endl;
        }
        else
        {
            std::shared_ptr<Manager> superior = GetSuperior();
            if (superior != NULL) // 其余的申请都需转到上级
            {
                superior->RequestApplications(request);
            }
        }
    }
};

// 总监类      总监所能有的权限就是可批准下属一周内的假期
class Majordomo : public Manager
{
public:
    Majordomo(const std::string &strName) : Manager(strName) {}
    virtual void RequestApplications(const Request &request)
    {
        if (request.requestType == "请假" && request.number <= 5)
        {
            std::cout << "" << GetName().c_str() << ":" << request.requestContent.c_str() << " 数量 " << request.number << " 被批准!" << std::endl;
        }
        else
        {
            std::shared_ptr<Manager> superior = GetSuperior();
            if (superior != NULL) // 其余的申请都需转到上级
            {
                superior->RequestApplications(request);
            }
        }
    }
};

// 总经理类
class GeneralManager : public Manager
{
public:
    GeneralManager(const std::string &strName) : Manager(strName) {}
    virtual void RequestApplications(const Request &request)
    {
        if (request.requestType == "请假" ||
            (request.requestType == "加薪" && request.number <= 500))
        {
            std::cout << "" << GetName().c_str() << ":" << request.requestContent.c_str() << " 数量 " << request.number << " 被批准!" << std::endl;
        }
        else if (request.requestType == "加薪" && request.number > 500)
        {
            std::cout << "" << GetName().c_str() << ":" << request.requestContent.c_str() << " 数量 " << request.number << " 被拒绝!" << std::endl;
        }
        else
        {
            std::cout << "" << GetName().c_str() << ":" << request.requestContent.c_str() << " 数量 " << request.number << " 无法处理!" << std::endl;
        }
    }
};

int main()
{
    std::shared_ptr<Manager> manager = std::make_shared<CommonManager>("经理");
    std::shared_ptr<Manager> major = std::make_shared<Majordomo>("总监");
    std::shared_ptr<Manager> generalManager = std::make_shared<GeneralManager>("总经理");
    // 设置上级
    manager->SetSuperior(major);
    major->SetSuperior(generalManager);

    std::cout << "员工申请请假结果:" << std::endl;
    Request request;
    request.requestType = "请假";
    request.number = 1;
    request.requestContent = "小菜申请请假1天";
    manager->RequestApplications(request);

    std::cout << std::endl
              << "员工申请加薪结果:" << std::endl;
    request.requestType = "请假";
    request.number = 4;
    request.requestContent = "小菜申请请假4天";
    manager->RequestApplications(request);

    std::cout << std::endl
              << "员工申请加薪结果:" << std::endl;
    request.requestType = "加薪";
    request.number = 400;
    request.requestContent = "小菜申请加薪400";
    manager->RequestApplications(request);

    std::cout << std::endl
              << "员工申请加薪结果:" << std::endl;
    request.requestType = "加薪";
    request.number = 1000;
    request.requestContent = "小菜申请加薪1000";
    manager->RequestApplications(request);

    system("pause");
    return 0;
}

C

c 复制代码
#include <stdio.h>
#include <stdlib.h>
typedef struct Request
{
    int days; // 请假天数
} Request;

typedef struct Manager
{
    char *name;
    struct Manager *superior;
    void (*handleRequest)(struct Manager *self, Request *req);
} Manager;

void commonManagerHandleRequest(Manager *self, Request *req);
void majordomoHandleRequest(Manager *self, Request *req);
void generalManagerHandleRequest(Manager *self, Request *req);

void commonManagerHandleRequest(Manager *self, Request *req)
{
    if (req->days <= 2)
    {
        printf("%s:%s 数量 %d 被批准!\n", self->name, "小菜申请请假", req->days);
    }
    else if (self->superior)
    {
        self->superior->handleRequest(self->superior, req);
    }
}

void majordomoHandleRequest(Manager *self, Request *req)
{
    if (req->days <= 5)
    {
        printf("%s:%s 数量 %d 被批准!\n", self->name, "小菜申请请假", req->days);
    }
    else if (self->superior)
    {
        self->superior->handleRequest(self->superior, req);
    }
}

void generalManagerHandleRequest(Manager *self, Request *req)
{
    if (req->days <= 10)
    {
        printf("%s:%s 数量 %d 被批准!\n", self->name, "小菜申请请假", req->days);
    }
    else
    {
        printf("%s:%s 数量 %d 被拒绝!\n", self->name, "小菜申请请假", req->days);
    }
}

Manager *createManager(char *name, void (*handleRequest)(Manager *, Request *))
{
    Manager *manager = malloc(sizeof(Manager));
    manager->name = name;
    manager->handleRequest = handleRequest;
    manager->superior = NULL;
    return manager;
}

void setSuperior(Manager *manager, Manager *superior)
{
    manager->superior = superior;
}

void freeManager(Manager *manager)
{
    free(manager);
}

int main()
{
    Manager *manager = createManager("经理", commonManagerHandleRequest);
    Manager *major = createManager("总监", majordomoHandleRequest);
    Manager *generalManager = createManager("总经理", generalManagerHandleRequest);

    setSuperior(manager, major);
    setSuperior(major, generalManager);

    Request request;

    printf("员工申请请假结果:\n");
    request.days = 1;
    manager->handleRequest(manager, &request);

    printf("\n员工申请请假结果:\n");
    request.days = 4;
    manager->handleRequest(manager, &request);

    printf("\n员工申请请假结果:\n");
    request.days = 10;
    manager->handleRequest(manager, &request);

    freeManager(manager);
    freeManager(major);
    freeManager(generalManager);

    return 0;
}
相关推荐
Funny-Boy1 小时前
Chain Responsibility Pattern
责任链模式
Uu_05kkq3 小时前
【C语言1】C语言常见概念(总结复习篇)——库函数、ASCII码、转义字符
c语言·数据结构·算法
嵌入式科普5 小时前
十一、从0开始卷出一个新项目之瑞萨RA6M5串口DTC接收不定长
c语言·stm32·cubeide·e2studio·ra6m5·dma接收不定长
A懿轩A5 小时前
C/C++ 数据结构与算法【栈和队列】 栈+队列详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·考研·算法·栈和队列
思忖小下6 小时前
梳理你的思路(从OOP到架构设计)_简介设计模式
设计模式·架构·eit
1 9 J7 小时前
数据结构 C/C++(实验五:图)
c语言·数据结构·c++·学习·算法
liyinuo20178 小时前
嵌入式(单片机方向)面试题总结
嵌入式硬件·设计模式·面试·设计规范
仍然探索未知中8 小时前
C语言经典100例
c语言
爱吃西瓜的小菜鸡8 小时前
【C语言】矩阵乘法
c语言·学习·算法
Stark、9 小时前
【Linux】文件IO--fcntl/lseek/阻塞与非阻塞/文件偏移
linux·运维·服务器·c语言·后端