设计模式之职责链模式(ChainOfResponsibility)的C++实现

1、职责链模式的提出

在软件开发过程中,发送者经常发送一个数据请求给特定的接收者对象,让其对请求数据进行处理(一个数据请求只能有一个对象对其处理)。如果发送的每个数据请求指定特定的接收者, 将带来发送者与接收者的紧密解耦合问题。职责链模式可以将请求发送者与接收者解耦。职责链模式的处理流程是:设计一个职责链处理基类,该类具有指向下一个待处理类的变量,通过set接口设置下一个待处理的链对象成员;该类具有遍历处理请求的功能,遍历过程中,只要有一个对象处理了请求,则停止遍历;该基类抽象了2个接口分别为:判断是否处理该类型请求和处理消息的功能接口。其他的具体职责链类继承并实现职责链基类的接口。

2、需求描述

有3个职责功能类,每个职责类都只能对特定的请求数据类型进行处理,现有3个请求类型。设计一个能处理不同数据请求的功能代码,该功能代码具有良好的扩展性。数据请求的类型包括:请求类型和描述信息。

3、功能实现

(1)UML图如下:

 

(2)代码实现如下:

#include <iostream>
#include <string>
enum class EmType
{
    EM_TYPE_A,
    EM_TYPE_B,
    EM_TYPE_C
};

class RequestData
{
private:
    std::string m_strDesc;
    EmType      m_emType;
public:
    RequestData(const std::string& desc,EmType type):m_strDesc(std::move(desc)),m_emType(type){};
    EmType  getType()const{return m_emType;};
    const std::string getDesc()const{return m_strDesc;};
};


class ChainHandler
{
protected:
    ChainHandler* nextChain{nullptr};
    virtual void processHandler(const RequestData& request)=0;
    virtual bool IsHandlerType(const RequestData& request)=0;
public:
    void setNextChain(ChainHandler* next)
    {
      nextChain = next;
    };

    void handle(const RequestData& req)
    {
        if(IsHandlerType(req))
        {
            processHandler(req);
        }else
        {
            if(nextChain != nullptr)
            {
                nextChain->handle(req);
            }
        }
    }
    virtual ~ChainHandler(){};
};

class HanderA:public ChainHandler
{
public:
    virtual bool IsHandlerType(const RequestData &request) override
    {
        if(request.getType() == EmType::EM_TYPE_A)
        {
            return true;
        }else
        {
            std::cout << "HanderA cannot process the request:" << request.getDesc() << std::endl;
            return false;
        }

    }
    virtual void processHandler(const RequestData& request) override
    {
        std::cout << "HanderA is processing the request: " << request.getDesc() << std::endl;
    }
};

class HanderB:public ChainHandler
{
public:
    virtual bool IsHandlerType(const RequestData &request) override
    {
        if(request.getType() == EmType::EM_TYPE_B)
        {
            return true;
        }else
        {
            std::cout << "HanderB cannot process the request:" << request.getDesc() << std::endl;
            return false;
        }
    }
    virtual void processHandler(const RequestData& request) override
    {
        std::cout << "HanderB is processing the request: " << request.getDesc() << std::endl;
    }
};

class HanderC:public ChainHandler
{
public:
    virtual bool IsHandlerType(const RequestData &request) override
    {
        if(request.getType() == EmType::EM_TYPE_C)
        {
            return true;
        }else
        {
            std::cout << "HanderC cannot process the request:" << request.getDesc() << std::endl;
            return false;
        }
    }
    virtual void processHandler(const RequestData& request) override
    {
        std::cout << "HanderC is processing the request: " << request.getDesc() << std::endl;
    }
};

class Client
{
public:
    void doWork()
    {
        ChainHandler* handlerA = new HanderA();
        ChainHandler* handlerB = new HanderB();
        ChainHandler* handlerC = new HanderC();
        handlerA->setNextChain(handlerB);
        handlerB->setNextChain(handlerC);

        RequestData reqA("DataA need to handle",EmType::EM_TYPE_A);
        handlerA->handle(reqA);
        std::cout << "\n************** next Request ********************\n" << std::endl;
        RequestData reqC("DataC need to handle",EmType::EM_TYPE_C);
        handlerA->handle(reqC);

        delete handlerA;
        delete handlerB;
        delete handlerC;

        handlerA = nullptr;
        handlerB = nullptr;
        handlerC = nullptr;
    }
};

int main()
{
    Client obj;
    obj.doWork();
    return 0;
}

 程序运行结果如下:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值