Friday, 16 July 2010

Chain of Responsibility Pattern

Purpose:




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.

When to use:
When more than one object may handle a request and the actual handler is not know in advance
When requests follow a “handle or forward” model - that is, some requests can be handled where they are generated while others must be forwarded to another object to be handled


Consequences
Reduced coupling between the sender of a request and the receiver – the sender and receiver have no explicit knowledge of each other
Receipt is not guaranteed - a request could fall off the end of the chain without being handled
The chain of handlers can be modified dynamically

Structure






Example: Java Servlet Filters













A filter intercepts the request before it gets to the requested resource. A response is returned to the client through the filter.


















Recommended books:

Head First Design PatternsJava(TM) Design Patterns: A Tutorial

0 comments:

Post a Comment