• 行为设计模式之职责链模式


    概述

    职责链模式(chain of responsibility pattern) 定义: 避免将一个请求的发送者与接收者耦合在一起,让多个对象都有机会处理请求.将接收请求的对象连接成一条链,并且沿着这条链传递请求,直到有一个对象能够处理它为止.

    职责链模式中,多个处理器(也就是刚刚定义中说的“接收对象”)依次处理同一个请 求。一个请求先经过 A 处理器处理,然后再把请求传递给 B 处理器,B 处理器处理完后再 传递给 C 处理器,以此类推,形成一个链条。链条上的每个处理器各自承担各自的处理职 责,所以叫作职责链模式。

    原理

    职责链模式结构:
    职责链结构图

    职责链模式主要包含以下角色:

    • 抽象处理者(Handler)角色:定义一个处理请求的接口,包含抽象处理方法和一个后继连接(链上的每个处理者都有一个成员变量来保存对于下一处理者的引用,比如上图中的successor) 。
    • 具体处理者(Concrete Handler)角色:实现抽象处理者的处理方法,判断能否处理本次请求,如果可以处理请求则处理,否则将该请求转给它的后继者。
    • 客户类(Client)角色:创建处理链,并向链头的具体处理者对象提交请求,它不关心处理细节和请求的传递过程。

    代码实现

    class Context {
    public:
        std::string name;
        int day;
    };
    
    class IHandler {
    public:
        virtual ~IHandler() {}
        void SetNextHandler(IHandler *next) { 
            next = next;
        }
    
        bool Handle(const Context &ctx) {
            if (CanHandle(ctx)) {
                return HandleRequest(ctx);
            } else if (GetNextHandler()) {
                return GetNextHandler()->Handle(ctx);
            } else {
                // err
            }
            return false;
        }
    protected:
        virtual bool HandleRequest(const Context &ctx) = 0;
        virtual bool CanHandle(const Context &ctx) =0;
        IHandler * GetNextHandler() {
            return next;
        }
    private:
        IHandler *next;
    };
    
    
    class HandleByMainProgram : public IHandler {
    protected:
        virtual bool HandleRequest(const Context &ctx){
            //
            return true;
        }
        virtual bool CanHandle(const Context &ctx) {
            //
            if (ctx.day <= 10)
                return true;
            return false;
        }
    };
    
    class HandleByProjMgr : public IHandler {
    protected:
        virtual bool HandleRequest(const Context &ctx){
            //
            return true;
        }
        virtual bool CanHandle(const Context &ctx) {
            //
            if (ctx.day <= 20)
                return true;
            return false;
        }
    };
    class HandleByBoss : public IHandler {
    protected:
        virtual bool HandleRequest(const Context &ctx){
            //
            return true;
        }
        virtual bool CanHandle(const Context &ctx) {
            //
            if (ctx.day < 30)
                return true;
            return false;
        }
    };
    
    class HandleByBeauty : public IHandler {
    protected:
        virtual bool HandleRequest(const Context &ctx){
            //
            return true;
        }
        virtual bool CanHandle(const Context &ctx) {
            //
            if (ctx.day <= 3)
                return true;
            return false;
        }
    };
    

    小结

    本篇主要写了职责链相关的内容,包括职责链的概念,职责链的原理,以及职责链的代码实现。还是挺详细的,部分内容来源于这里,有兴趣可以去看看。当然,还是要自己去积累一些。OK,结束。

  • 相关阅读:
    明天就是PMP考试了(6月25日),这些大家都了解了吗?
    IIC总线
    大数据之Hive(二)
    大模型时代的基础设施:云原生峰会 KubeCon 2023 中国站重磅来袭!
    数据的内存分布笔记总结
    YOLOv7训练:_pickle.UnpicklingError: STACK_GLOBAL requires str
    Python Flask flasgger api文档[python/flask/flasgger]
    对 three.js webgl_clipping_stencil 例子的理解
    C++学习——优先级队列模拟实现与仿函数初步认识
    iNFTnews | Web3时代,用户将拥有数据自主权
  • 原文地址:https://blog.csdn.net/Vjunjun/article/details/139251760