责任链模式

本文介绍了责任链模式,通过实例展示了如何使用该模式改进请假流程的审批逻辑,避免冗余的if-else语句。通过创建一个处理者链,每个节点根据条件决定是否处理请求,否则传递给下一个节点。这种方法提高了代码可维护性和扩展性,方便自定义处理顺序,简化了新增逻辑的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

责任链模式

定义

使多个对象都有机会处理请求,从而避免请求的发送着和接受着之间的耦合关系,将这些对象连成一条链,并沿着这条链传递请求,知道有一个对象处理它为止

请假流程,1天内需要主程序批准,3天内需要项目经理批准,3天以上需要老板批准,意思就是说根据条件如何合适就执行逻辑,不符合那么就继续寻找下一个节点

直接上代码:

#include <iostream>
using namespace std;

class Context {
public:
    std::string name;
    int day;
};

class LeaveRequest {
public:
    // 随着判断的增加,LeaveRequest类变得不稳定
    bool HandleRequest(const Context &ctx) {
        if (ctx.day <= 3)
           return HandleByMainProgram(ctx);
        else if (ctx.day <= 10)
           return HandleByProjMgr(ctx);
        else
           return HandleByBoss(ctx);

        return false;
    }

private:
    bool HandleByMainProgram(const Context &ctx) {
        return true;
    }
    bool HandleByProjMgr(const Context &ctx) {
        return true;
    }
    bool HandleByBoss(const Context &ctx) {
        return true;
    }
};

int main(){
    Context s;
    s.day = 30;
    LeaveRequest la;
    bool result = la.HandleRequest(s);
    
    std::cout<< result <<std::endl;

    return 0;
}

如代码所示 Context 是用传递上下文,LeaveRequest类中判断是多少天,然后交给响应的函数,分别处理这些任务,但是从分流的过程中发现十分繁琐,一个个if,else十分的臃肿。如果都条件改了,那么

由于交给谁审批这个是不知道的,所以可以对其进行抽象,用一个链式的结构进行保存,遍历这个链表如果当前节点能处理当前节点就处理该问题,如果不行就交给下一个节点。

#include <iostream>
using namespace std;

class Context {
public:
    std::string name;
    int day;
};


class IHandler{
public:
    virtual ~IHandler(){

    }
    
    IHandler():_next(nullptr){}
    void SetNextHandle(IHandler *next){
        _next = next;
    }
    
    bool Handler(const Context& xt){
        if(CanHandle(xt)){
            return HandleRuest(xt);
        }
        else if(GetNextHandle()){
            return GetNextHandle()->Handler(xt);
        }
        else{
            return 0;  //可以返回错误
        }
    }

protected:
    virtual bool CanHandle(const Context& xt) = 0;
    virtual bool HandleRuest(const Context& xt) = 0;
    IHandler *GetNextHandle(){
        return _next;
    }

private:
    IHandler *_next;
};


class HandlerByProjMgr : public IHandler{
    protected: 
    virtual bool CanHandle(const Context& xt){
        if(xt.day < 3){
            return true;
        }
            return false;
    }
    
    virtual bool HandleRuest(const Context& xt){
        std::cout << "我是 projmgr 同意"<<std::endl;
        return true;
    }

};

class HandlerByBoss : public IHandler{
    protected:
        virtual bool CanHandle(const Context& xt){
         if(xt.day < 6){
            return true;
        }
            return false;
        }
    
    virtual bool HandleRuest(const Context& xt){
        std::cout << "我是 Boss 同意"<<std::endl;
        return true;
    }
};

class HandlerByCeo : public IHandler{
    protected:
         virtual bool CanHandle(const Context& xt){
            if(xt.day < 10){
            return true;
        }
            return false;
    }
    
    virtual bool HandleRuest(const Context& xt){
        std::cout << "我是 ceo 同意"<<std::endl;
        return true;
    }
};


int main(){
    IHandler *h1 = new HandlerByProjMgr;
    IHandler *h2 = new HandlerByBoss;
    IHandler *h3 = new HandlerByCeo;
    
    h1->SetNextHandle(h2);
    h2->SetNextHandle(h3);
    
    Context *xt = new Context();
    xt->day = 9;

    bool ret = h1->Handler(*xt);
    std::cout << "ret: " <<ret<<std::endl;
    
    return 0;
}

各位不要怕代码变多了,仔细看这个逻辑很简单,本人的博客的所有代码都可以直接复制下来运行。 你可以试试将 xt->day 随便修改值,很容易发现,程序会自动判断并选择相应的节点进行处理。

这样不仅可以自定义处理节点的顺序,而且新增逻辑的时候不需要进行再加if了很方便。

即设计模式的使用说白了就是 封装固定点, 抽象变化点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值