职责链模式

对这一模式很感兴趣;
在前面的观察者模式中,我将它与现有的处理CAN报文的方式做了比较。但是在看到 “职责链模式” 后,觉得这一模式更加适合替代原有的 “链表+回调函数” ,起到简化代码的处理;

#include <iostream>

using namespace std;

//尝试实现“责任链”模式;任务在责任链中循环,知道遇到可以处理它的部分;
class NodeOfChain
{
public:
    NodeOfChain() {
        std::cout<<"i am nodeofchain constructor"<<endl;
    }
    //处理数据的方式;
    virtual void HanderMessage(string &tmpStr) = 0;
    //实现下一个节点;
    //这里用引用不好,因为实际工程中,常常用new实例化一个类;
    void SetNextNode(NodeOfChain *tmpNode)
    {
        NextNode = tmpNode;
    }
    virtual ~NodeOfChain()
    {
        std::cout<<"i am nodeofchain disconstructor"<<endl;
    }
    static int done;
protected:
    NodeOfChain *NextNode = nullptr;
private:
    //私有变量、函数不可被子类继承;
};
int NodeOfChain::done = 0;
//左手处理;
class LeftHand : public NodeOfChain
{
public:
    LeftHand(const string &val1){
        std::cout<<val1<<endl;
        std::cout<<"i am letfhand constructor"<<endl;
    }
    //符合格式则处理数据,否则交给下一个节点处理;
    void HanderMessage(string &tmpstr)
    {
        if(string::npos != tmpstr.find('a'))
        {
            std::cout<<"i am hander a now"<<std::endl;
            std::cout<<tmpstr<<std::endl;
            ++done;
        }else {
            NextNode->HanderMessage(tmpstr);
        }
    }
    ~LeftHand()
    {
        std::cout<<"i am letfhand disconstructor"<<endl;
    }

};
//右手处理
class RightHand : public NodeOfChain
{
public:
    RightHand(){
        std::cout<<"i am righthand consstrcutor"<<endl;
    }
    void HanderMessage(string& tmpstr)
    {
        if(string::npos != tmpstr.find('b'))
        {
            std::cout<<"i am hander b now"<<std::endl;
            std::cout<<tmpstr<<std::endl;
            ++done;
        }else {
            NextNode->HanderMessage(tmpstr);
        }
    }
    ~RightHand()
    {
        std::cout<<"i am righthand disconstrcutor"<<endl;
    }
};
//头处理
class HandHander : public NodeOfChain
{
public:
    HandHander() {
        std::cout<<"i am hend constructor"<<endl;
    }
    void HanderMessage(string & tmpstr)
    {
        if(string::npos != tmpstr.find('c'))
        {
            std::cout<<"i am hander c now"<<std::endl;
            std::cout<<tmpstr<<std::endl;
            ++done;
        }else {
            NextNode->HanderMessage(tmpstr);
        }
    }
    ~HandHander()
    {
        std::cout<<"i am hend disconstructor"<<endl;
    }
};

int main()
{
    const string name = "hander left message";
    NodeOfChain *left = new LeftHand(name);
    NodeOfChain *right = new RightHand();
    NodeOfChain *Hand = new HandHander();

    left->SetNextNode(right);
    right->SetNextNode(Hand);

    string mes = "csdfg";
    left->HanderMessage(mes);
    if(0 == NodeOfChain::done)
    {
        std::cout<<" sorry , message is not read"<<endl;

    }

    delete left;
    delete right;
    delete Hand;
}

简述:在上方的代码中,用左手、右手、头分别代表不同的处理信息的方式;每个节点尝试处理报文,不成功则调用下一个节点处理,直到报文处理成功或者节点遍历完毕。
辨析:
优点:显而易见,逻辑简单,容易维护;
缺点:①无探底—若已添加节点不可处理报文,则函数卡死;
②很难柔性的添加节点;(我没想到)
————————辣鸡分割线————————————
一、static的使用;
①static不可以用于external;
②非const类型的static不可以声明的同时初始化;
③注意声明和初始化的方法(这一点一直纸上谈兵,没有落到实处)
二、虚函数,虚函数表,虚析构
这部分放在C++ 栏目内单独叙述;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值