Chain of Responsibility -- 责任链模式

在软件构建构成中,一个请求可能被多个对象处理,但是每个请求在运行时只能有一个接收者,如果显示指定,将必不可少地带来请求发送者与接收者的紧密耦合。COR(Chain of Reposibility)设计模式可以使请求的发送者不需要指定具体的接收者,让请求的接收者自己在运行时决定来处理请求,从而使两者解耦。

使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象对应它为止。
“Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.” - GoF

实例解析
例如就拿我们工作中的事情来说,就拿报销来说,一个组长的审批权力为500元及其以下,他都可以处理这些报销请求,当超过500元时候,他就没有权利审批了,就需要到部门经理那里了,经理可以审批5000元及其以下,如果超过5000元就需要找公司副经理了,他可以审批50000元及其以下了,当超过50000元,就需要找公司经理来审批了,这里假定是500000元,好吧你如果说超过了5000000元,怎么办?我想国内还没有公司愿意为你报500000元,再说你自己也不愿意拿500000元出来先垫着。好了这里不考虑超过500000元的情况,那么我们通过责任链模式来实现这一场景吧。
那么先做一个表格
级别 权限
组长 500元
部门经理 5000元
公司副经理 50000元
公司经理 500000元

#include <iostream>

class Request
{
    public:
        Request(double m)
        :money(m)
        {};
        double getMoney(){return money;}
    private:
        double money;  
};

class IHandle
{
    public:
        IHandle(IHandle *p)
        :nextHandle(p)
        {}
        virtual bool IsAuthority(Request r)=0;
        virtual void doHandle(Request r)=0;
    protected:
        IHandle * nextHandle;
    
};

class headmanHandle:public IHandle
{
    public:
        headmanHandle(IHandle *p)
        :IHandle(p)
        {}
        bool IsAuthority(Request r);
        void doHandle(Request r);
};
bool headmanHandle::IsAuthority(Request r)
{
    if(r.getMoney()>500)return false;
    else return true;
}
void headmanHandle::doHandle(Request r)
{
    if(IsAuthority(r))
    {
        std::cout<<"The headman do the request,which the request money is"<<r.getMoney()<<std::endl;
    }
    else
    {
        nextHandle->doHandle(r);
    }
}


class DepartmentMHandle:public IHandle
{
    public:
        DepartmentMHandle(IHandle *p)
        :IHandle(p)
        {}
        bool IsAuthority(Request r);
        void doHandle(Request r);
};
bool DepartmentMHandle::IsAuthority(Request r)
{
    if(r.getMoney()>5000)return false;
    else return true;
}
void DepartmentMHandle::doHandle(Request r)
{
    if(IsAuthority(r))
    {
        std::cout<<"The department manager do the request,which the request money is"<<r.getMoney()<<std::endl;
    }
    else
    {
        nextHandle->doHandle(r);
    }
}


class CompanyDMHandle:public IHandle
{
    public:
        CompanyDMHandle(IHandle *p)
        :IHandle(p)
        {}
        bool IsAuthority(Request r);
        void doHandle(Request r);
};
bool CompanyDMHandle::IsAuthority(Request r)
{
    if(r.getMoney()>50000)return false;
    else return true;
}
void CompanyDMHandle::doHandle(Request r)
{
    if(IsAuthority(r))
    {
        std::cout<<"The company deputy manager do the request,which the request money is"<<r.getMoney()<<std::endl;
    }
    else
    {
        nextHandle->doHandle(r);
    }
}


class CompanyMHandle:public IHandle
{
    public:
        CompanyMHandle(IHandle *p)
        :IHandle(p)
        {}
        bool IsAuthority(Request r);
        void doHandle(Request r);
};
bool CompanyMHandle::IsAuthority(Request r)
{
    if(r.getMoney()>500000)return false;
    else return true;
}
void CompanyMHandle::doHandle(Request r)
{
    if(IsAuthority(r))
    {
        std::cout<<"The company manager do the request,which the request money is "<<r.getMoney()<<std::endl;
    }
    else
    {
        std::cout<<"You are the boss of thie company!Because you use up"<<r.getMoney()<<std::endl;
    }
}

int main(int argc,char ** argv)
{
    IHandle *p4=new CompanyMHandle(NULL);
    IHandle *p3=new CompanyDMHandle(p4);
    IHandle *p2=new DepartmentMHandle(p3);
    IHandle *p1=new headmanHandle(p2);
    Request r1(300);
    Request r2(3000);
    Request r3(30000);
    Request r4(300000);
    Request r5(3000000);
    p1->doHandle(r1);
    p1->doHandle(r2);
    p1->doHandle(r3);
    p1->doHandle(r4);
    p1->doHandle(r5);
    delete p1;
    delete p2;
    delete p3;
    delete p4;
    return 0;
}



运行结果为


如果有兴趣可以继续浏览该系列文章:
singleton pattern--单件模式
factory mothed pattern--工厂方法模式
abstract factory pattern--抽象工厂模式
builder pattern--建造者模式
prototype pattern--原型模式
adapter pattern--适配器模式
bridge pattern -- 桥接模式
composite pattern -- 组合模式
decorator pattern -- 装饰模式
decorator pattern -- 享元模式
decorator pattern -- 代理模式
decorator pattern -- 责任链模式
  • 21
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值