重读设计模式——职责链

设计#

职责链是行为型设计模式中的一种,关注的是请求发送对象和请求接收对象间的关系,目的是使一个请求被发出后,多个有关联的对象都可能有机会处理这个请求,避免了请求发送者和接收者间的耦合关系。让所有接收者连成一条链,然后请求在这条链上进行传播,直至有对象处理为止,而这些对象可以被动态地绑定。

示例代码#

enum class ReqType : char {
    null,
    type1,
    type2,
    type3
};

class ChainHandler {
public:
    ChainHandler(ChainHandler* successor, ReqType type)
        : m_successor(successor), m_type(type) { }

public:
    virtual void handleReq(ReqType req) = 0;

protected:
    virtual void nextHandler(ReqType req) {
        if(m_successor != nullptr) {
            m_successor->handleReq(req);
        }
    }

protected:
    ReqType m_type{ ReqType::null };

private:
    ChainHandler* m_successor{ nullptr };
};

class Chain1 : public ChainHandler {
public:
    Chain1(ChainHandler* successor, ReqType type)
        : ChainHandler(successor, type) { }

    void handleReq(ReqType req) override {
        if(req == m_type) {
            cout << "Chain1" << endl;
        }
        else {
            nextHandler(req);
        }
    }
};

class Chain2 : public ChainHandler {
public:
    Chain2(ChainHandler* successor, ReqType type)
        : ChainHandler(successor, type) { }

    void handleReq(ReqType req) override {
        if(req == m_type) {
            cout << "Chain2" << endl;
        }
        else {
            nextHandler(req);
        }
    }
};

class Chain3 : public ChainHandler {
public:
    Chain3(ChainHandler* successor, ReqType type)
        : ChainHandler(successor, type) { }

    void handleReq(ReqType req) override {
        if(req == m_type) {
            cout << "Chain3" << endl;
        }
        else {
            nextHandler(req);
        }
    }
};

int main() {
    Chain1 c1(nullptr, ReqType::type1);
    Chain2 c2(&c1, ReqType::type2);
    Chain3 c3(&c2, ReqType::type3);

    c3.handleReq(ReqType::type3);
    c3.handleReq(ReqType::type2);
    c3.handleReq(ReqType::type1);

    return 0;
}

首先定义了一个职责链的基类,它负责将处理的请求传递给继承者。所有链上的职责对象在创建时都需要指定自己的继承者。
当一个请求被职责链上的对象处理时——简单起见,我们用一个枚举来表示当前对象可以处理的消息,以及发送的消息,当前对象首先判断自己是否可以处理这个请求,如果可以就处理——这里用打印当前类名来表示,不可以就传递给它的继承者,直至可以被处理或丢弃。
按照main函数中的调用,会得到如下结果:

Chain3
Chain2
Chain1

说明三次调用的请求都被正确地处理了。

作者:cwtxx

出处:https://www.cnblogs.com/cwtxx/p/18718233

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   cwtxx  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示