欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

职责链(Chain of Responsibility)模式

程序员文章站 2022-06-08 21:16:36
...

一、职责链(Chain of Responsibility)模式

责任链模式是一种对象的行为模式【GOF95】。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递, 直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新 组织链和分配责任。

从击鼓传花谈起

击鼓传花是一种热闹而又紧张的饮酒游戏。在酒宴上宾客依次坐定位置,由一人击鼓,击鼓的地方与传花的地方是分开的,以示公正。开始击鼓时,花束就开始依次传递,鼓声一落,如果花束在某人手中,则该人就得饮酒。

击鼓传花便是责任链模式的应用。责任链可能是一条直线、一个环链或者一个树结构的一部分



二、责任链模式的结构

责任链模式涉及到的角色如下所示:

职责链(Chain of Responsibility)模式

抽象处理者(Handler)角色:定义出一个处理请求的接口。如果需要,接口可以定义出一个方法,以设定和返回对下家的引用。这个角色通常由一个抽象类或接口实现。

具体处理者(ConcreteHandler)角色:具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。由于具体处理者持有对下家的引用,因此,如果需要,具体处理者可以访问下家。


三、责任链模式的示意性源代码

职责链(Chain of Responsibility)模式//ChainofResponsibilitypattern--Structuralexample
职责链(Chain of Responsibility)模式
usingSystem;
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//"Handler"
职责链(Chain of Responsibility)模式
abstractclassHandler
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//Fields
职责链(Chain of Responsibility)模式
protectedHandlersuccessor;
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Methods
职责链(Chain of Responsibility)模式
publicvoidSetSuccessor(Handlersuccessor)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式this.successor=successor;
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式abstractpublicvoidHandleRequest(intrequest);
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//"ConcreteHandler1"
职责链(Chain of Responsibility)模式
classConcreteHandler1:Handler
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//Methods
职责链(Chain of Responsibility)模式
overridepublicvoidHandleRequest(intrequest)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式if(request>=0&&request<10)
职责链(Chain of Responsibility)模式Console.WriteLine("{0}handledrequest{1}",
职责链(Chain of Responsibility)模式this,request);
职责链(Chain of Responsibility)模式else
职责链(Chain of Responsibility)模式if(successor!=null)
职责链(Chain of Responsibility)模式successor.HandleRequest(request);
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//"ConcreteHandler2"
职责链(Chain of Responsibility)模式
classConcreteHandler2:Handler
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//Methods
职责链(Chain of Responsibility)模式
overridepublicvoidHandleRequest(intrequest)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式if(request>=10&&request<20)
职责链(Chain of Responsibility)模式Console.WriteLine("{0}handledrequest{1}",
职责链(Chain of Responsibility)模式this,request);
职责链(Chain of Responsibility)模式else
职责链(Chain of Responsibility)模式if(successor!=null)
职责链(Chain of Responsibility)模式successor.HandleRequest(request);
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//"ConcreteHandler3"
职责链(Chain of Responsibility)模式
classConcreteHandler3:Handler
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//Methods
职责链(Chain of Responsibility)模式
overridepublicvoidHandleRequest(intrequest)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式if(request>=20&&request<30)
职责链(Chain of Responsibility)模式Console.WriteLine("{0}handledrequest{1}",
职责链(Chain of Responsibility)模式this,request);
职责链(Chain of Responsibility)模式else
职责链(Chain of Responsibility)模式if(successor!=null)
职责链(Chain of Responsibility)模式successor.HandleRequest(request);
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式///<summary>
职责链(Chain of Responsibility)模式
///Clienttest
职责链(Chain of Responsibility)模式
///</summary>

职责链(Chain of Responsibility)模式publicclassClient
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式publicstaticvoidMain(string[]args)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//SetupChainofResponsibility
职责链(Chain of Responsibility)模式
Handlerh1=newConcreteHandler1();
职责链(Chain of Responsibility)模式Handlerh2=newConcreteHandler2();
职责链(Chain of Responsibility)模式Handlerh3=newConcreteHandler3();
职责链(Chain of Responsibility)模式h1.SetSuccessor(h2);
职责链(Chain of Responsibility)模式h2.SetSuccessor(h3);
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Generateandprocessrequest
职责链(Chain of Responsibility)模式
int[]requests={2,5,14,22,18,3,27,20};
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式foreach(intrequestinrequests)
职责链(Chain of Responsibility)模式h1.HandleRequest(request);
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式}


四、纯的与不纯的责任链模式

一个纯的责任链模式要求一个具体的处理者对象只能在两个行为中选择一个:一个是承担责任,二是把责任推给下家。不允许出现某一个具体处理者对象在承担了一部分责任后又把责任向下传的情况。

在一个纯的责任链模式里面,一个请求必须被某一个处理者对象所接收;在一个不纯的责任链模式里面,一个请求可以最终不被任何接收端对象所接收。纯的责任链模式的例子是不容易找到的,一般看到的例子均是不纯的责任链模式的实现。


五、责任链模式的实际应用案例

下面的责任链模式代码演示了不同职务的人根据所设定的权限对一个购买请求作出决策或将其交给更高的决策者。

职责链(Chain of Responsibility)模式//ChainofResponsibilitypattern--RealWorldexample
职责链(Chain of Responsibility)模式
usingSystem;
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//"Handler"
职责链(Chain of Responsibility)模式
abstractclassApprover
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//Fields
职责链(Chain of Responsibility)模式
protectedstringname;
职责链(Chain of Responsibility)模式protectedApproversuccessor;
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Constructors
职责链(Chain of Responsibility)模式
publicApprover(stringname)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式this.name=name;
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Methods
职责链(Chain of Responsibility)模式
publicvoidSetSuccessor(Approversuccessor)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式this.successor=successor;
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式abstractpublicvoidProcessRequest(PurchaseRequestrequest);
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//"ConcreteHandler"
职责链(Chain of Responsibility)模式
classDirector:Approver
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//Constructors
职责链(Chain of Responsibility)模式
publicDirector(stringname):base(name){}
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Methods
职责链(Chain of Responsibility)模式
overridepublicvoidProcessRequest(PurchaseRequestrequest)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式if(request.Amount<10000.0)
职责链(Chain of Responsibility)模式Console.WriteLine("{0}{1}approvedrequest#{2}",
职责链(Chain of Responsibility)模式this,name,request.Number);
职责链(Chain of Responsibility)模式else
职责链(Chain of Responsibility)模式if(successor!=null)
职责链(Chain of Responsibility)模式successor.ProcessRequest(request);
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//"ConcreteHandler"
职责链(Chain of Responsibility)模式
classVicePresident:Approver
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//Constructors
职责链(Chain of Responsibility)模式
publicVicePresident(stringname):base(name){}
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Methods
职责链(Chain of Responsibility)模式
overridepublicvoidProcessRequest(PurchaseRequestrequest)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式if(request.Amount<25000.0)
职责链(Chain of Responsibility)模式Console.WriteLine("{0}{1}approvedrequest#{2}",
职责链(Chain of Responsibility)模式this,name,request.Number);
职责链(Chain of Responsibility)模式else
职责链(Chain of Responsibility)模式if(successor!=null)
职责链(Chain of Responsibility)模式successor.ProcessRequest(request);
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//"ConcreteHandler"
职责链(Chain of Responsibility)模式
classPresident:Approver
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//Constructors
职责链(Chain of Responsibility)模式
publicPresident(stringname):base(name){}
职责链(Chain of Responsibility)模式//Methods
职责链(Chain of Responsibility)模式
overridepublicvoidProcessRequest(PurchaseRequestrequest)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式if(request.Amount<100000.0)
职责链(Chain of Responsibility)模式Console.WriteLine("{0}{1}approvedrequest#{2}",
职责链(Chain of Responsibility)模式this,name,request.Number);
职责链(Chain of Responsibility)模式else
职责链(Chain of Responsibility)模式Console.WriteLine("Request#{0}requires"+
职责链(Chain of Responsibility)模式"anexecutivemeeting!",request.Number);
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Requestdetails
职责链(Chain of Responsibility)模式
classPurchaseRequest
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//MemberFields
职责链(Chain of Responsibility)模式
privateintnumber;
职责链(Chain of Responsibility)模式privatedoubleamount;
职责链(Chain of Responsibility)模式privatestringpurpose;
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Constructors
职责链(Chain of Responsibility)模式
publicPurchaseRequest(intnumber,
职责链(Chain of Responsibility)模式doubleamount,stringpurpose)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式this.number=number;
职责链(Chain of Responsibility)模式this.amount=amount;
职责链(Chain of Responsibility)模式this.purpose=purpose;
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Properties
职责链(Chain of Responsibility)模式
publicdoubleAmount
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式get{returnamount;}
职责链(Chain of Responsibility)模式set{amount=value;}
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式publicstringPurpose
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式get{returnpurpose;}
职责链(Chain of Responsibility)模式set{purpose=value;}
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式publicintNumber
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式get{returnnumber;}
职责链(Chain of Responsibility)模式set{number=value;}
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式///<summary>
职责链(Chain of Responsibility)模式
///ChainAppApplication
职责链(Chain of Responsibility)模式
///</summary>

职责链(Chain of Responsibility)模式publicclassChainApp
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式publicstaticvoidMain(string[]args)
职责链(Chain of Responsibility)模式{
职责链(Chain of Responsibility)模式//SetupChainofResponsibility
职责链(Chain of Responsibility)模式
DirectorLarry=newDirector("Larry");
职责链(Chain of Responsibility)模式VicePresidentSam=newVicePresident("Sam");
职责链(Chain of Responsibility)模式PresidentTammy=newPresident("Tammy");
职责链(Chain of Responsibility)模式Larry.SetSuccessor(Sam);
职责链(Chain of Responsibility)模式Sam.SetSuccessor(Tammy);
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式//Generateandprocessdifferentrequests
职责链(Chain of Responsibility)模式
PurchaseRequestrs=newPurchaseRequest(2034,350.00,"Supplies");
职责链(Chain of Responsibility)模式Larry.ProcessRequest(rs);
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式PurchaseRequestrx=newPurchaseRequest(2035,32590.10,"ProjectX");
职责链(Chain of Responsibility)模式Larry.ProcessRequest(rx);
职责链(Chain of Responsibility)模式
职责链(Chain of Responsibility)模式PurchaseRequestry=newPurchaseRequest(2036,122100.00,"ProjectY");
职责链(Chain of Responsibility)模式Larry.ProcessRequest(ry);
职责链(Chain of Responsibility)模式}

职责链(Chain of Responsibility)模式}

六、责任链模式的实现

责任链模式并不创建责任链。责任链的创建必须由系统的其它部分创建出来。

责任链模式降低了请求的发送端和接收端之间的耦合,使多个对象都有机会处理这个请求。一个链可以是一条线,一个树,也可以是一个环。如下图所示,责任链是一个树结构的一部分。

职责链(Chain of Responsibility)模式