责任链模式
责任链模式
使多个对象都有机会处理请求,从而避免请求的发送者
和接收者之间的耦合关系。将这些对象连成一条链,并
沿着这条链传递该请求,直到有一个对象处理它为止。
Chain of Responsibility Pattern
Chain of Responsibility Pattern
CChhaaiinn ooff RReessppoonnssiibbiilliittyy PPaatttteerrnn
Avoid coupling the sender of a request to its
Avoid coupling the sender of a request to its
AAvvooiidd ccoouupplliinngg tthhee sseennddeerr ooff aa rreeqquueesstt ttoo iittss
receiver by giving more than one object a chance
receiver by giving more than one object a chance
rreecceeiivveerr bbyy ggiivviinngg mmoorree tthhaann oonnee oobbjjeecctt aa cchhaannccee
to handle the request. Chain the receiving objects
to handle the request. Chain the receiving objects
ttoo hhaannddllee tthhee rreeqquueesstt.. CChhaaiinn tthhee rreecceeiivviinngg oobbjjeeccttss
and pass the request along the chain until an
and pass the request along the chain until an
aanndd ppaassss tthhee rreeqquueesstt aalloonngg tthhee cchhaaiinn uunnttiill aann
object handles it.
object handles it.
oobbjjeecctt hhaannddlleess iitt..
2011-11-29
1
概 述
使多个对象都有机会处理请求,从而避免请求
的发送者和接收者之间的耦合关系。将这些对象连
成一条链,这条链传递该请求,直到有一个对象处
理它为止。这一模式的想法是,给多个对象处理一
个请求的机会,从而解耦发送者和接受者.
2011-11-29
2
责任链模式的结构与使用
模式的结构中包括两种角色:
• 处理者(Handler)
• 具体处理者(ConcreteHandler)
2011-11-29
3
模式的UML类图
2011-11-29
4
参与者
1.Handler
定义一个处理请求的接口。实现后继链。
2.ConcreteHandler
处理它所负责的请求。可访问它的后继者。如
果可处理该请求,就处理;否则将该请求转发
给它的后继者。
3.Client
向链上的具体处理者(Concret
原创力文档

文档评论(0)