Chain of Responsibility Design Pattern

This is kind of Behavioral Design Pattern(BDP) and as per the BDP definition, “Behavioral patterns describe how the objects behave when they interact with each other“. So this design pattern is used when the job is done in pieces and those pieces interact with each other at run time.

E.g.  Controller -> Validator -> Service -> DAOImpls.

So as you know, Controllers are the entry point of any MVC application. The flow become like below.

  • Controllers take the inputs,
  • Inputs are validated in Validators,
  • Validated inputs are passed to the Service layer
  • If nothing wrong in Service layer, inputs are passed to DAO Layer.

You can think of a use case that you want to debit some amount from your bank account. So the flow will be like

  • You pass your account number along with amount, you want to withdraw,
  • Account number is validated,
  • The remaining balance is validated against what you are requesting for,
  • Once validation passed, further process happens.

Another real time example I can think of is javax.servlet.Filter#doFilter() methods.

Hope this clears you doubt!!

One thought on “Chain of Responsibility Design Pattern

Comments are closed.