Post1

Understanding Chain of Responsibility Pattern

Posted by Abdul Shami Khan

The chain of responsibility helps to create a chain of receiver objects to complete a request. This pattern decouples the sender and receiver of every request based on the style of the request. Every receiver contains a reference to another receiver and if an object is unable to handle any request, it passes on the request to the next receiver. The passing of responsibilities continues until an object completes the request.

Application of Chain of Responsibility

There are several instances when we need to consider the chain of responsibility pattern, such as

  • When there’s a need to decouple the sender and receiver of a request.
  • When multiple objects can handle a request but can only be determined at the runtime.
  • When handlers are not specified explicitly in code.
  • When there’s a need to issue a request to one of several objects without explicitly specifying the receiver.
  • When there are multiple objects to handle a request but the handler doesn’t have to be a specific object and the handler is determined at runtime.

Examples of Chain of Responsibility

ATM, Vending Machine, Exception Handling, Logger and Servlet are some of the finest examples of the chain of responsibility pattern.

Out of all the examples, exception handling is the easiest to understand for a programmer. A sequence of exceptions are listed in the catch statements when an exception is thrown, the catch list is scanned line by line to find the appropriate exception to handle the job until then the responsibility is passed to the next exception in the catch.

Drawbacks of Chain of Responsibility

  • It is very hard to debug at runtime characteristic.
  • There’s no guarantee of execution of the request, as no handler handles the request due to wrong chain sequence.
  • It gives loose coupling but has numerous implementation classes, which have a common code, making it difficult to maintain coupling.

UML Diagram

Where,

Handler – defines an interface required to handle the request.

Concrete Handler – handles the request of the sequence it is part of and if it cannot handle the request, then it passes the request to its successor.

Client – source of the request and accesses the handler to handle it.

Code Example

//Request Object

//Concrete Handler 1

//Concrete Handler 2

//Concrete Handler 3

//Client code

Conclusion

As discussed in the blog post, the Chain of Responsibility is a highly practical approach and we are surrounded by its applications. This makes it imperative for software professionals to be aware of the concept of the Chain of Responsibility pattern design. With advancing technology, we expect the concept of Chain of Responsibility pattern design will evolve and become more dynamic and widespread.

That is it from us. Let us know your thoughts in the comments below.

Until next time!

Related Posts

  • Content Data Store

    Content Data Store Content Data Store (CDS) is a system to provide storage facilities to massive data sets in the form of images, pdfs, documents and scanned documents. This dataset…

  • Flyway Integration with Spring MVCFlyway Integration with Spring MVC

    Flyway is an open-source database migration tool that supports simplicity and convention over configuration. It makes setting up and maintaining database schemas a breeze. Flyway is used across all environments…

  • In a Nutshell: JAVA Executor FrameworkIn a Nutshell: JAVA Executor Framework

    Java executor framework is a very useful mechanism introduced in Java 1.5 to manage threads in large-scale applications. Before the executor framework, multithreading applications were developed using thread group and…

  • Infinispan Cache: In a NutshellInfinispan Cache: In a Nutshell

    One of the hottest new additions to JBoss galaxy is Infinispan. It is an open source, Java-based data grid platform. Data grids are highly concurrent distributed data structures that allow users to address large…

  • AI in Banking and Financial Services (AML)AI in Banking and Financial Services (AML)

    Recently HSBC announced that it is implementing AI technology to automate AML investigations - (Source: Reuters 01-June-2017) Similarly, many more banks have started using AI technology in various fields. Since…

  • 10 Steps to Build a Time Series Model10 Steps to Build a Time Series Model

    As promised in our previous blog “Introduction to Time Series Forecasting in R,” we are back with our next installment to build a time series model. Time series modeling is…

Leave a Reply

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