GoF Chain of Responsibility Pattern

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.

Componentized Chain of Responsibility Pattern

PerfectJPattern's componentized version of the Chain of Responsibility Pattern provides the following advantages:

  • Parameterizes the Handler interface with generic request parameter, making the pattern type-safe and flexible to user-defined context-specific request definitions.
  • Clear separation of concerns as handling logic is separated from decision-making logic i.e. handle() vs. start(), see The Chain of Responsibility pattern's pitfalls and improvements article.
  • Flexible decision-making with the Strategy Pattern e.g. decision on whether to forward requests to successor Handler instances. IHandler instances may be configured with a IChainStrategy that parameterize the Chain-continuation behavior. It was provided two concrete implementations AllHandleStrategy and OnlyOneHandleStrategy where the later is the use-case covered in the original GoF implementation and the default Strategy.

UML Class Design



  • Two flavors of Handler IHandler and IParameterlessHandler definitions: Providing both user-defined single-parameter request types and Chain that do not require any request parameter.
  • NullHandler implementation: that completely avoids dealing with null values as Chain stop condition.
  • Ready to use AbstractHandler and AbstractParameterlessHandler base abstract types: that only require subtypes providing the Handler business handle(...) implementation.