Tuesday, August 26, 2008

Chain of Responsibility Pattern

The classic Chain of Responsibility(CoR) pattern defined by Gang of Four(GoF) in Design Patterns:

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


Following Figure illustrates the class diagram.

Figure 1. CoR class diagram

A typical object structure might look like following Figure .

Figure 2. CoR object structure

From the above illustrations, we can summarize that:

  • Multiple handlers may be able to handle a request
  • Only one handler actually handles the request
  • The requester knows only a reference to one handler
  • The requester doesn't know how many handlers are able to handle its request
  • The requester doesn't know which handler handled its request
  • The requester doesn't have any control over the handlers
  • The handlers could be specified dynamically
  • Changing the handlers list will not affect the requester's code
Uses of CoR:
1. Microsoft Windows global hook framework
2. Java servlet filter framework


Details are explained by Michael Xinsheng Huang, JavaWorld.com, 08/16/04

No comments: