chain of responsibilit职责链模式
动机:在软件构建过程中,一个请求可能被多个对象处理,但是每个请求在运行时只能有一个
接收者,如果显示指定,将必不可少地带来请求发送者和接收者的深耦合.
意图:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.
将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止.
chain of responsibility 模式的应用场合在于“一个请求可能有多个接收者,但是最后真正
的接受者只有一个”,只有这时候请求发送者与接受者的偶合才有可能出现“变化脆弱”的症状,
职责链的母的旧式将二者解耦,从而更好的应对变化
应用了chain of responsibilit模式后,对象的职责分派将更具灵活性。我们可以在运行时动态添加
修改请求的处理职责。
如果请求传递到职责链的末尾依然得不到处理,应该有一个合理的缺省机制,这也是美一个接受对象
的责任,而不是发出请求的对象的责任。
first
接收者,如果显示指定,将必不可少地带来请求发送者和接收者的深耦合.
意图:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.
将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止.
chain of responsibility 模式的应用场合在于“一个请求可能有多个接收者,但是最后真正
的接受者只有一个”,只有这时候请求发送者与接受者的偶合才有可能出现“变化脆弱”的症状,
职责链的母的旧式将二者解耦,从而更好的应对变化
应用了chain of responsibilit模式后,对象的职责分派将更具灵活性。我们可以在运行时动态添加
修改请求的处理职责。
如果请求传递到职责链的末尾依然得不到处理,应该有一个合理的缺省机制,这也是美一个接受对象
的责任,而不是发出请求的对象的责任。
first
abstract class BaseHandler
{
public abstract bool CanHandleRequest();
public abstract void HandleRequest(Request request);
private BaseHandler next;
public BaseHandler Next
{
get
{
return this.next;
}
set
{
this.next = value;
}
}
}
public class AHandler : BaseHandler
{
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
}
}
public class BHandler : BaseHandler
{
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
}
}
public class CHandler : BaseHandler
{
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
}
}
public class Sender
{
public void Process()
{
Request request = new Request();
ArrayList list = new ArrayList();
list.Add(new AHandler() );
list.Add(new BHandler() );
list.Add(new CHandler() );
foreach(BaseHandler handler in list)
{
if (handler.CanHandleRequest())
{
handler.HandleRequest(request);
}
}
}
}
{
public abstract bool CanHandleRequest();
public abstract void HandleRequest(Request request);
private BaseHandler next;
public BaseHandler Next
{
get
{
return this.next;
}
set
{
this.next = value;
}
}
}
public class AHandler : BaseHandler
{
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
}
}
public class BHandler : BaseHandler
{
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
}
}
public class CHandler : BaseHandler
{
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
}
}
public class Sender
{
public void Process()
{
Request request = new Request();
ArrayList list = new ArrayList();
list.Add(new AHandler() );
list.Add(new BHandler() );
list.Add(new CHandler() );
foreach(BaseHandler handler in list)
{
if (handler.CanHandleRequest())
{
handler.HandleRequest(request);
}
}
}
}
abstract class BaseHandler
{
public BaseHandler(BaseHandler next)
{
this.next = next;
}
public abstract bool CanHandleRequest();
//public abstract void HandleRequest(Request request);
public virtual void HandleRequest(Request request)
{
if (this.next != null)
{
this.next.HandleRequest(request);
}
}
private BaseHandler next;
public BaseHandler Next
{
get
{
return this.next;
}
set
{
this.next = value;
}
}
}
public class AHandler : BaseHandler
{
public AHandler(BaseHandler next)
: base(next)
{
}
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
if (this.CanHandleRequest())
{
}
else
{
base.HandleRequest(request);
}
}
}
public class BHandler : BaseHandler
{
public BHandler(BaseHandler next)
: base(next)
{
}
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
}
}
public class CHandler : BaseHandler
{
public CHandler(BaseHandler next)
: base(next)
{
}
public override bool CanHandleReques()
{
if()
{
//
}
else
{
//
}
}
public override void HandleRequest(Request request)
{
}
}
public class Sender
{
public void Process()
{
//Request request = new Request();
Sender sender = new Sender;
Handler handler1 = new AHandler(null);
Handler handler2 = new BHandler(handler1);
Handler handler3 = new CHandler(handler2);
handler3.HandlerRequest(request);
sender.Process(handler3);
}
}
{
public BaseHandler(BaseHandler next)
{
this.next = next;
}
public abstract bool CanHandleRequest();
//public abstract void HandleRequest(Request request);
public virtual void HandleRequest(Request request)
{
if (this.next != null)
{
this.next.HandleRequest(request);
}
}
private BaseHandler next;
public BaseHandler Next
{
get
{
return this.next;
}
set
{
this.next = value;
}
}
}
public class AHandler : BaseHandler
{
public AHandler(BaseHandler next)
: base(next)
{
}
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
if (this.CanHandleRequest())
{
}
else
{
base.HandleRequest(request);
}
}
}
public class BHandler : BaseHandler
{
public BHandler(BaseHandler next)
: base(next)
{
}
public override bool CanHandleReques()
{
}
public override void HandleRequest(Request request)
{
}
}
public class CHandler : BaseHandler
{
public CHandler(BaseHandler next)
: base(next)
{
}
public override bool CanHandleReques()
{
if()
{
//
}
else
{
//
}
}
public override void HandleRequest(Request request)
{
}
}
public class Sender
{
public void Process()
{
//Request request = new Request();
Sender sender = new Sender;
Handler handler1 = new AHandler(null);
Handler handler2 = new BHandler(handler1);
Handler handler3 = new CHandler(handler2);
handler3.HandlerRequest(request);
sender.Process(handler3);
}
}