• C++设计模式 职责链模式(Chain of Resposibility)


    数据结构模式

    • 常常有一-些组件在内部具有特定的数据结构,如果让客户程序依赖这些特定的数据结构,将极大地破坏组件的复用。这时候,将这些特定数据结构封装在内部,在外部提供统一的接口,来实现与特定数据结构无关的访问,是一种行之有效的解决方案。

    典型模式

    Chain of Resposibility

    动机( Motivation )

    • 在软件构建过程中, -个请求可能被多个对象处理,但是每个请求在运行时只能有一个接受者,如果显式指定,将必不可少地带来请求发送者与接受者的紧耦合。
    • 如何使请求的发送者不需要指定具体的接受者?让请求的接受者自己在运行时决定来处理请求 ,从而使两者解耦。

    模式定义

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

    结构

    在这里插入图片描述

    要点总结

    • Chain of Responsibility模式的应用场合在于“一个请求可能有,多个接受者,但是最后真正的接受者只有一个”, 这时候请求发送者与接受者的耦合有可能出现“变化脆弱”的症状,职责链的目的就是将二者解耦,从而更好地应对变化。
    • 应用了Chain of Responsibility模式后,对象的职责分派将更具灵活性。我们可以在运行时动态添加/修改请求的处理职责。
    • 如果请求传递到职责链的末尾仍得不到处理,应该有一个合理的,缺省机制。这也是每一个接受对象的责任,而不是发出请求的对象的责任。

    cpp

    #include<iostream>
    #include<string>
    
    enum class RequestType
    {
        REQ_HANDLER1,
        REQ_HANDLER2,
        REQ_HANDLER3
    };
    
    class Reqest
    {
    public:
        Reqest(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
    {
    public:
        ChainHandler() :nextChain(nullptr) {}
        void setNextChain(ChainHandler* next) { nextChain = next; }
    
        void handle(const Reqest& req)
        {
            if (this->canHandleRequest(req))
            {
                this->processRequest(req);
            }
            else
            {
                this->sendReqestToNextHandler(req);
            }
        }
    
    protected:
        virtual bool canHandleRequest(const Reqest&) = 0;
        virtual void processRequest(const Reqest&) = 0;
    
    private:
        void sendReqestToNextHandler(const Reqest& req)
        {
            if (nextChain != nullptr)
            {
                nextChain->handle(req);
            }
        }
    
    private:
        ChainHandler* nextChain;
    };
    
    class Handler1 :public ChainHandler
    {
    public:
        virtual bool canHandleRequest(const Reqest& req)
        {
            return req.getReqType() == RequestType::REQ_HANDLER1;
        }
        virtual void processRequest(const Reqest& req)
        {
            std::cout << "Handler1 is handle reqest: " << req.getDescription() << std::endl;
        }
    };
    class Handler2 : public ChainHandler {
    protected:
        bool canHandleRequest(const Reqest& req) override
        {
            return req.getReqType() == RequestType::REQ_HANDLER2;
        }
        void processRequest(const Reqest& req) override
        {
            std::cout << "Handler2 is handle reqest: " << req.getDescription() << std::endl;
        }
    };
    
    class Handler3 : public ChainHandler {
    protected:
        bool canHandleRequest(const Reqest& req) override
        {
            return req.getReqType() == RequestType::REQ_HANDLER3;
        }
        void processRequest(const Reqest& req) override
        {
            std::cout << "Handler3 is handle reqest: " << reqc.getDescription() << std::endl;
        }
    };
    
    int main()
    {
        Handler1 h1;
        Handler2 h2;
        Handler3 h3;
        h1.setNextChain(&h2);
        h2.setNextChain(&h3);
    
        Reqest req("process task ... ", RequestType::REQ_HANDLER3);
        h1.handle(req);
        return 0;
    }
    
  • 相关阅读:
    CI框架源码解析十九之分页类文件Pagination.php
    sublime新代码段
    sublime
    递归调用详解,分析递归调用的详细过程
    什么是锚点?锚点应该如何用?
    PHP中spl_autoload_register函数的用法
    PHP 构造方法 __construct()
    工欲善其事,必先利其器---IDE使用
    Apache操作
    js中常用的操作
  • 原文地址:https://www.cnblogs.com/chengmf/p/16042135.html
Copyright © 2020-2023  润新知