美文网首页
设计模式之职责链模式

设计模式之职责链模式

作者: coolTigers | 来源:发表于2019-12-27 23:43 被阅读0次

职责链模式使得多个对象都有机会处理请求,将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
结构图如下:


image.png

一个典型的对象结构可能如下图所示:


image.png
假设请求有多种,并且有多种handler动作与之对应。
头文件chainOfResponsibility.h定义如下:
#pragma once
#ifndef CHAIN_OF_RESPONSIBILITY_H
#define CHAIN_OF_RESPONSIBILITY_H
#include <string>
#include <iostream>
enum RequestType
{
    REQ_HANDLER1 = 0,
    REQ_HANDLER2,
    REQ_HANDLER3
};
class Request
{
public:
    Request(const std::string& desc, RequestType type):description(desc), reqType(type) {}
    RequestType getReqType() const { return reqType; }
    const std::string& getDescription() const { return description; }
private:
    std::string description;
    RequestType reqType;
};

class ChainHandler {
private:
    ChainHandler* nextChain;
    void sendRequestToNextHandler(const Request& req) {
        if (nextChain != nullptr) {
            nextChain->handle(req);
        }
    }
protected:
    virtual bool canHandleRequest(const Request& req) = 0;
    virtual void processRequest(const Request& req) = 0;
public:
    ChainHandler() {
        nextChain = nullptr;
    }
    void setNextChain(ChainHandler* next) {
        nextChain = next;
    }
    void handle(const Request& req) {
        if (canHandleRequest(req)) {
            processRequest(req);
        } else {
            sendRequestToNextHandler(req);
        }
    }

};

class Handler1:public ChainHandler {
protected:
    bool canHandleRequest(const Request& req) {
        return req.getReqType() == RequestType::REQ_HANDLER1;
    }
    void processRequest(const Request& req) {
        std::cout << "Handle1 is handle request!" << req.getDescription() << std::endl;
    }
};

class Handler2 :public ChainHandler {
protected:
    bool canHandleRequest(const Request& req) {
        return req.getReqType() == RequestType::REQ_HANDLER2;
    }
    void processRequest(const Request& req) {
        std::cout << "Handle2 is handle request!" << req.getDescription() << std::endl;
    }
};

class Handler3 :public ChainHandler {
protected:
    bool canHandleRequest(const Request& req) {
        return req.getReqType() == RequestType::REQ_HANDLER3;
    }
    void processRequest(const Request& req) {
        std::cout << "Handle3 is handle request!" << req.getDescription() << std::endl;
    }
};
#endif // !CHAIN_OF_RESPONSIBILITY_H

定义了一个请求Request类,一个handle抽象基类,三个handle具体子类。每个子类重写了匹配请求类型的canHandleRequest方法和处理请求的processRequest方法。
调用程序main.cpp如下:

#include "chainOfResponsibility.h"

using namespace std;

int main()
{
    Handler1 h1;
    Handler2 h2;
    Handler3 h3;
    h1.setNextChain(&h2);
    h2.setNextChain(&h3);

    Request req("process task...", RequestType::REQ_HANDLER3);
    h1.handle(req);
}

这里首先定义了三个具体handle对象,并通过setNextChain方法使得形成了h1->h2->h3结构的职责链。
接着定义了一个Request对象(handle3)。
h1.handle(req);语句调用函数顺序如下:

ChainHandler::handle
      Handler1::canHandleRequest(false)
      ChainHandler::sendRequestToNextHandler
            ChainHandler::handle
                  Handler2::canHandleRequest(false)
                  ChainHandler::sendRequestToNextHandler
                         ChainHandler::handle
                                 Handler3::canHandleRequest(true)
                                 Handler3::processRequest

输出结果如下:


image.png

本文主要源于了李建忠的《设计模式》课程。

相关文章

  • 设计模式-工具链模式和迭代器模式

    设计模式-职责链模式和迭代器模式 1.职责链模式 设计动机客户端发出一个请求,职责链上的对象都可以处理这一请求,而...

  • 设计模式之职责链模式

    在业务开发中,为了处理某个属性,可能需要复杂的处理逻辑,才能得到,一般的做法是按步骤处理,最终得到想要的结果。作者...

  • 设计模式之职责链模式

    职责链模式 责任链模式是一种设计模式。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求...

  • 设计模式之职责链模式

    定义 职责链模式(Chain of Responsibility Pattern):避免请求发送者与接收者耦合在...

  • 设计模式之职责链模式

    职责链模式使得多个对象都有机会处理请求,将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。结...

  • 设计模式之职责链模式

    定义 职责链模式(Chain of responsibility)是使多个对象都有机会处理请求,从而避免请求的发送...

  • iOS面试之设计模式模块

    设计模式 设计模式内容如下: 责任链模式 桥接模式 命令模式 适配器模式 单例模式 等等 设计原则 单一职责原则 ...

  • Typescript 职责链模式(Chain of Respon

    标签: 前端 设计模式 职责链模式 typescript Chain of Responsibility 请仔细阅...

  • 设计模式值职责链模式(行为型)

    [TOC] 一、行为型模式 介绍职责链模式之前先介绍一下行为型设计模式,因为按照GoF模式分类,职责链就是一种行为...

  • Java设计模式系列-责任链模式

    原创文章,转载请标注出处:《Java设计模式系列-责任链模式》 一、概述 职责链模式(称责任链模式)将请求的处理对...

网友评论

      本文标题:设计模式之职责链模式

      本文链接:https://www.haomeiwen.com/subject/llujoctx.html