8/06/2011

Design Pattern - Chain of Responsibility

Definition
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.

UML Class Diagram

Chain of Responsibility: when and where use it
- An important goal of object-oriented design is to keep objects loosely coupled by limiting their dependencies and keeping the relationships between objects specific and minimal.  Loosely coupled objects have the advantage that they are easier to maintain and easier to change compared to systems where there is tight coupling between objects (i.e. hard-coded references to specific classes).  

- The Chain of Responsibility design pattern offers an opportunity to build a collection of loosely coupled objects by relieving a client from having to know which objects in a collection can satisfy a request by arranging these objects in a chain.  This pattern requires a way to order the search for an object that can handle the request. This search is usually modeled according to the specific needs of the application domain.  Note that Chain-of-Responsibility is not commonly used in business application development. 

Sample
- Object class

- Handler interface

- Concrete Handler




- Invoke class

Ref: Gang of Four tutorial

No comments:

Post a Comment