编程语言
首页 > 编程语言> > C++设计模式——职责链模式

C++设计模式——职责链模式

作者:互联网

职责链模式(Chain of Responsibility):使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

 

 

优点

是当客户提交一个请求时,请求会沿着链传递知道一个concretehandler对对象进行处理。换句话说,就是不管处理请求的对象在那里,只要你把请求给职责链,它都会被处理,是的接收者和发送都没又对象的没有对方的明确信息,且链中对象都不知道这个链的结构。结果是职责链可以简化对象的相关连接。他们仅仅保持一个指向后继者的引用,而不用指向它所有接收者的引用。

缺点

暴露在当一个请求可能走到链的末端都得不到处理的情况,或者因为没有正确的配置而得不到处理,所以在使用职责链模式时,需要考虑到各种情况需要处理它的对象类。

 

#include <iostream>
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;
}

 

 

参考——大话设计模式

参考——https://blog.csdn.net/DU_YULIN/article/details/119911430

标签:std,superior,const,职责,request,C++,RequestApplications,设计模式,string
来源: https://www.cnblogs.com/Galesaur-wcy/p/15917914.html