Chain of Responsibility Design Pattern in Java

The Chain of Responsibility is known as a behavioral pattern, as it is used to manage algorithms, relationships and responsibilities between objects. The Chain of Responsibility pattern consists of a series of processing objects where each object containing the type of commands it can handle. Each object in the series either does its own task or passes to the next object if it cannot handle the request. A mechanism also exists for adding new processing objects to the end of this chain or series.

In this pattern objects in the chain are linked, typically as a one-directional linked list, with a next reference from one node to the next node. The request is passed along the chain, from one object to the next, until an object (a handler or receiver) handles it. When the request is handled, it is no longer passed on to the next level. If no object handles the request, a default object at the end of the chain can be made to handle it, or it may be that no object handles the request. Sometimes, if required, multiple objects may handle the request.

Class Diagram

Chain of Responsibility Pattern in Java

Example

Exception handling in Java is based on Chain of Responsibility pattern.

So in this pattern:

Sender does not know which object in the chain will serve its request
Each node in the chain may decide to serve the request such as catching an exception and wrapping it with an application specific exception
Each node can forward the request like throwing exception to the immediate caller
None of the node can serve the request such as leaves the job with the caller

Suppose the code written throws an StringIndexOutOfBoundsException. This exception will get caught at correct level when an exception occurs due to some bug in source code. Suppose, we have an exception which is an application specific in the catch block. This will not be caught by what we have an application specific exception. It will find for an Exception class and will be caught by that as both the application specific exception and the StringIndexOutOfBoundsException are sub-classes of the class Exception.

Once the exception gets caught, then it will not look for any other exception. Now this is the reason, sometimes why we get an “Exception is unreachable” message when we try to add a catch block with the exception below the parent exception.

Implementation

I will show you steps for placing an order for an online product seller. Steps for placing an order are shown as below:

1. Select a Product from online store
2. Register yourself for placing an order
3. Enter your billing or shipping address where your product will be delivered
4. Make payment for your order
5. Get the confirmation message once your order has been successfully placed

Create an interface

Create a class

Create five different implementation classes for placing an order

Create a test class for testing the pattern

Output

That’s all. Thank you for your reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.