职责链模式 -- C++

职责链模式

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

  • 目的:对请求的发送者和接受者进行解耦。
  • 属于:“对象行为型模式”。

示例


#ifndef DESIGNEPATTERNS_REQUEST_H
#define DESIGNEPATTERNS_REQUEST_H

#include <iostream>
#include <string>

enum class RequestType {
    REQ_HAN1,
    REQ_HAN2,
    REQ_HAN3
};

class Request {

public:
    Request(const std::string &desc, RequestType type) : description(desc), requesttype(type) {}

    virtual ~Request() {}

    RequestType getRequesttype() const {
        return requesttype;
    }

    const std::string &getDescription() const {
        return description;
    }

private:
    std::string description;

    RequestType requesttype;

};


#endif //DESIGNEPATTERNS_REQUEST_H

#ifndef DESIGNEPATTERNS_HANDLER_H
#define DESIGNEPATTERNS_HANDLER_H

#include "Request.h"

class Handler {

public:
    Handler() { m_nextHandler = nullptr; }

    /**
     * 设置下一个节点
     * @param next
     */
    void setNextHandler(Handler *next) {
        m_nextHandler = next;
    }

    /**
     * 处理函数,如果能处理则处理当前节点,若不能处理给下一个节点
     * @param req
     */
    void handle(const Request req) {
        if (canHandleReq(req))
            processReq(req);
        else
            sendToNextHandler(req);
    }

protected:
    virtual bool canHandleReq(const Request &req) = 0;  // ①
    virtual void processReq(const Request &req) = 0;  // ②
private:
    Handler *m_nextHandler;    // 下一个指针

    /**
     * 发送给下一个节点处理
     * @param req
     */
    void sendToNextHandler(const Request &req) {
        if (m_nextHandler != nullptr)
            m_nextHandler->handle(req);
    }
};


#endif //DESIGNEPATTERNS_HANDLER_H

#ifndef DESIGNEPATTERNS_HANDLE1_H
#define DESIGNEPATTERNS_HANDLE1_H

#include "handler.h"

class Handle1 : public Handler {
protected:
    bool canHandleReq(const Request &req) override {
        return RequestType::REQ_HAN1 == req.getRequesttype();
    }

    void processReq(const Request &req) override {
        std::cout << "Handle1::processReq: " << req.getDescription() << std::endl;
    }
};

#endif //DESIGNEPATTERNS_HANDLE1_H

#ifndef DESIGNEPATTERNS_HANDLE2_H
#define DESIGNEPATTERNS_HANDLE2_H

#include "handler.h"

class Handle2 : public Handler {
protected:
    bool canHandleReq(const Request &req) override {
        return RequestType::REQ_HAN2 == req.getRequesttype();
    }

    void processReq(const Request &req) override {
        std::cout << "Handle2::processReq: " << req.getDescription() << std::endl;
    }
};

#endif //DESIGNEPATTERNS_HANDLE2_H

#ifndef DESIGNEPATTERNS_HANDLE3_H
#define DESIGNEPATTERNS_HANDLE3_H

#include "handler.h"

class Handle3 : public Handler {
protected:
    bool canHandleReq(const Request &req) override {
        return RequestType::REQ_HAN3 == req.getRequesttype();
    }

    void processReq(const Request &req) override {
        std::cout << "Handle3::processReq: " << req.getDescription() << std::endl;
    }
};

#endif //DESIGNEPATTERNS_HANDLE3_H


#ifndef DESIGNEPATTERNS_INCLIB_H
#define DESIGNEPATTERNS_INCLIB_H

#include "handle1.h"
#include "handle2.h"
#include "handle3.h"

void CORTest() {
    Handler *h1 = new Handle1();
    Handler *h2 = new Handle2();
    Handler *h3 = new Handle3();

    h1->setNextHandler(h2);
    h2->setNextHandler(h3);

    Request req("meimei", RequestType::REQ_HAN2);

    h1->handle(req);
}

#endif //DESIGNEPATTERNS_INCLIB_H

#include <iostream>
#include "ChainOfResponsibility/incLib.h"

int main() {
    // ChainOfResponsibility test
    CORTest();

    
    return 0;
}

结果

职责链模式 -- C++

上一篇:日志输出流较佳实践[较佳实践]


下一篇:Java蓝桥杯——凯撒密码单词加密