责任链模式(大话设计模式)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;
}
相关推荐
哪 吒3 小时前
最简单的设计模式,抽象工厂模式,是否属于过度设计?
设计模式·抽象工厂模式
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
网易独家音乐人Mike Zhou3 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
转世成为计算机大神6 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
搬砖的小码农_Sky6 小时前
C语言:数组
c语言·数据结构
小乖兽技术7 小时前
23种设计模式速记法
设计模式
小白不太白9508 小时前
设计模式之 外观模式
microsoft·设计模式·外观模式
小白不太白9509 小时前
设计模式之 原型模式
设计模式·原型模式
澄澈i9 小时前
设计模式学习[8]---原型模式
学习·设计模式·原型模式
ahadee10 小时前
蓝桥杯每日真题 - 第19天
c语言·vscode·算法·蓝桥杯