Overview

Chain of Responsibility Pattern can be used when we want to give more than one object a chance to handle a request. The pattern can be used to achieve loose coupling in software design where the request can be passed through a chain of objects to process them, Based on state of chain object it will handle the request or pass it to the next chain object.

Chain of Responsibility
Chain of Responsibility

As per the diagram, each request pass through the chain of handlers and it will flow through the chain until it gets handled by any of the handler.

Let’s understand with Example

ABC Organization has been getting more email than they can handle since the release of the Java powered Gumball Machine. From their own analysis they get four kinds of email: fan mail from customers that love the new game, complaints from parents whose kids are addicted to the game, requests to put machines in new locations. They also get a spam mails.

All fan mail needs to go straight to the CEO, all complaints go to the legal department and all requests for new machines go to business development. Spam needs to be deleted.

Mail Example with Chain of Responsibility Pattern
Mail Example with Chain of Responsibility Pattern

Implementation

We have created Handler Interface which is having aforwardMail() method to forward mail to the corresponding department. We have created 4 chain handlers, They implement the Handler Interface. Each chain Handler checks subject of Mail and accordingly it will handle the request or pass it to the next handler.

Step 1 :

Create a Mail.java class

Step 2 :

Create a base Interface.

Handler.java
Step 3 :

Create a chain Handler implementing base Inteface.

SpamHandler.java FanHandler.java ComplaintHandler.java NewLocHandler.java
Step 4 :

Create different types of chain Handler and set next Handler to form a chain. Next handler in each Handler represents the part of the chain.

Output :
Mail Deleted. Forwarding Mail to CEO. Forwarding Mail to Legal Department. Forwarding Mail to Business Development Group.

As email is received, it is passed to the handler: the SpamHandler. If the SpamHandler can’t handle the request, It is passed on to the FanHandler. And so on…

Benefits of Chain of Responsibility Pattern
  1. Decouples the sender of the request and its receivers.
  2. Simplifies our object because it doesn’t have to know the chain’s structure and keep direct references to its memebers.
  3. Allows to add or remove responsibilities dynamically by changing the memebers or order of the chain.
Drawbacks of Chain of Responsibility Pattern
  1. Execution of the request isn’t guaranteed; it may fail off the end of the chain if no object handles it.
  2. Can be hard to observe the run-time characteristics and debug.
It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

2 Thoughts on “Chain of Responsibility Design Pattern in Java”

  • How about combining COR with modified Builder pattern and ending up with something like:

    chain.getChainBuilder().setNextChain(fanHandlerObj).setNextChain(complaingtHandlerObj).setNextChain(newLocngtHandlerObj).build();

    How about that 🙂

    • Hi Bali,

      Glad to know that you have inputs here.
      We generally follow a practice to write a code which is readable and understandable to readers. But what you are saying is also right way to build a chain.

Leave a Reply

Your email address will not be published. Required fields are marked *