标签:
Chain Of Responsibility 职责链模式(行为型模式)
请求的发送者与接受者
某些对象请求的接受者可能有多种多样,变化无常……
动机(Motivation)
在软件构建过程中,一个请求可能被多个对象处理,但是每个请求在运行时只能有一个接受者,如果显示指定,将必不可少地带来请求发送者与接受者的紧耦合。
如何使请求的发送者不需要指定具体的接受者?让请求的接受者自己在运行时决定来处理请求,从而使两者解耦。
意图(Intent)
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止。——《设计模式》GoF
示例代码
public abstract class BaseHandler { public abstract void HandleRequest(Request request); public abstract bool CanHandleRequest(Request request); } public class AHandler : BaseHandler { public override void HandleRequest(Request request) { //... } public override bool CanHandleRequest(Request request) { //... } } public class BHandler : BaseHandler { public override void HandleRequest(Request request) { //... } public override bool CanHandleRequest(Request request) { //... } } public class CHandler : BaseHandler { public override void HandleRequest(Request request) { //... } public override bool CanHandleRequest(Request request) { //... } } public class Request { //... } public class Sender { public void Process() { Request request=new Request(); //... List<BaseHandler> handlerList=new List<BaseHandler>(); handlerList.Add(new AHandler()); handlerList.Add(new BHandler()); handlerList.Add(new CHandler()); foreach (var handler in handlerList) { if (handler.CanHandleRequest(request)) { handler.HandleRequest(request); } } } }
如果我们不希望Sender中有这么多的Handler的调用,可以通过指责链模式修改:
public abstract class BaseHandler { public BaseHandler(BaseHandler next) { this.Next = next; } public BaseHandler Next { get; set; } protected abstract bool CanHandleRequest(Request request); public virtual void HandleRequest(Request request) { if (this.Next != null) { this.Next.HandleRequest(request); } } } public class AHandler : BaseHandler { public AHandler(BaseHandler next) : base(next) { } public override void HandleRequest(Request request) { if (this.CanHandleRequest(request)) { //... } else { base.HandleRequest(request); } } protected override bool CanHandleRequest(Request request) { //... } } public class BHandler : BaseHandler { public BHandler(BaseHandler next) : base(next) { } public override void HandleRequest(Request request) { //... } protected override bool CanHandleRequest(Request request) { //... } } public class CHandler : BaseHandler { public CHandler(BaseHandler next) : base(next) { } public override void HandleRequest(Request request) { //... } protected override bool CanHandleRequest(Request request) { //... } } public class Request { //... } public class Sender { public void Process(BaseHandler handler) { Request request = new Request(); //... handler.HandleRequest(request); } }
客户端调用:
static void Main(string[] args) { Sender sender=new Sender(); BaseHandler handler1 = new AHandler(null); BaseHandler handler2 = new BHandler(handler1); BaseHandler handler3 = new CHandler(handler2); sender.Process(handler3); Console.ReadKey(); }
结构(Structure)
一个典型的对象结构可能如下图所示:
Handler——定义一个处理请求的接口。(可选)实现后继链。
ConcreteHandler——处理它所负责的请求。可访问它的后继者。如果可处理该请求,就处理之;否则将该请求转发给它的后继者。
Client——向链上的具体处理者(ConcreteHandler)对象提交请求。
Chain Of Responsibility 模式的几个要点
转载请注明出处:
作者:JesseLZJ
出处:http://jesselzj.cnblogs.com
设计模式19:Chain Of Responsibility 职责链模式(行为型模式)
标签:
原文地址:http://www.cnblogs.com/jesselzj/p/4774276.html