责任链模式(大话设计模式)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 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
Peter_chq3 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
hikktn5 小时前
如何在 Rust 中实现内存安全:与 C/C++ 的对比分析
c语言·安全·rust
观音山保我别报错5 小时前
C语言扫雷小游戏
c语言·开发语言·算法
捕鲸叉6 小时前
MVC(Model-View-Controller)模式概述
开发语言·c++·设计模式
wrx繁星点点7 小时前
享元模式:高效管理共享对象的设计模式
java·开发语言·spring·设计模式·maven·intellij-idea·享元模式
凉辰7 小时前
设计模式 策略模式 场景Vue (技术提升)
vue.js·设计模式·策略模式
菜菜-plus7 小时前
java设计模式之策略模式
java·设计模式·策略模式
暗黑起源喵7 小时前
设计模式-迭代器
设计模式
小林熬夜学编程8 小时前
【Linux系统编程】第四十一弹---线程深度解析:从地址空间到多线程实践
linux·c语言·开发语言·c++·算法