Chain of Responsibility Pattern

  • We have loosely connected components with each other (That means the sender and receiver does not know from whom do they take the request or response),
  • Sender can pass the object for first component,
  • The sent object is processes by each component sequentially.
  • When the Handler1 receives the HandleRequest, it tries to process it. If it can not processs, then pass to the Handler2.
  • If Handler2 can process it, then it processes and otherwise sent it to the Handler3.
  • Likewise, it goes until the request is handle by the concrete handler even for last handler in sequence.
  • You can see in above code snippet, our 3 concrete handler classes implement all the properties and methods on its Parent Handler class.
  • Therefore, all 3 classes have implemented both setNextObject() and processSelection() with its parameters in it’s constructor via super().
  • As well, inside the each Handler class, they implemented their own implementation by based on marking schema.
  • Provide loosely-coupled relation over different component.
  • The chain does not require to know the structure and it just provides references to members.
  • The request can go even for the last handler until the request it handled.
  • It is hard to debug the runtime errors.
  • If the request does not get a handler, then the request can be dropped.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sanduni Jayaweera

Sanduni Jayaweera

Associate Software Engineer at Virtusa.