Chain of Responsibility Design Pattern

What is Chain of Responsibility Design Pattern?

  • It falls under behavioral design pattern.
  • It passes the request between multiple objects.
  • Avoid coupling the sender of a request to its receiver by giving more than one receiver object a chance to handle the request.
  • Chain the receiving objects and pass the request along with the chain until an object handles it.
  • It creates a chain of receiver objects for a request.
  • Each receiver contains a reference to another receiver. If one receiver cannot handle the request then it will pass to the next receiver.
  • (One receiver handles a request in the chain) or (One or more receivers in the chain handles a request).

Table of Contents

Real Life Examples of Chain of Responsibility Design Pattern

Example 1:

Quiz sample photoFor example, we will consider a quiz competition. Lenin is conducting a quiz competition and Raja, Chinchu and Sudha are the quiz competitors. They are trying to attend the quiz competition. So first Lenin is asking a question to Raja. Let’s suppose; Raja doesn’t know the answer to that question. Raja will transfer that question to Chenchu. Chenchu will try to answer that question. Let’s suppose if Chenchu doesn’t know the answer about that question. Then he will try to forward that question to Sudha. Here the request is being forwarded to others receivers if the receiver has no idea to reply to that request.

Example 2:

servers photoLet’s take an example if you are using Amazon website and you are trying to log into Amazon website using username and password. The client sends your credentials to the server. So, here client isn’t aware of Server details.Chain of servers would handle that request. Let’s consider a first server which handles the permission-related job. The next server handles the billing process and the next n number of servers handle some other jobs.Here, the single request has been handled by the chain of servers.

Example 3:

ATM Sample PhotoATM has various money handlers like 500 Rupee, 100 Rupee, and 50 Rupee handlers. Let’s suppose if you are trying to withdraw 650 Rupees from ATM.Then that request would be sent to the 500Rupee handler. It handles some part of the request and forwards the request to other handlers like 100Ruppes and 50 Rupees handlers. Finally, ATM serves us the required amount.

UML Class Diagram of Chain of Responsibility Design Pattern

Chain of Responsibility Design Pattern Java Implementation

  • In India, people get 240V electricity by default.
  • Mobile battery requires the only 3V for charging the battery.
  • Here, the Mobile charger is acting as an adapter to convert 240V to 3V.
  • We are implementing an adapter class “SocketAdapterImpl” while would have various methods to get the required voltage. 

Step 1:

public class SalesRep1 extends SalesRep
{

    @Override
    public void hanleJob()
    {
        System.out.println("Handling Gold");
    }
    
}

Step 2:

public class SalesRep2 extends SalesRep
{

    @Override
    public void hanleJob()
    {
        System.out.println("Handling dress");
    }
    
}

Step 3:

public class SalesRep3 extends SalesRep
{

    @Override
    public void hanleJob()
    {
        System.out.println("Handling toys");
    }
    
}

Step 4:

public abstract class SalesRep
{
    public abstract void hanleJob();
}

Tutorial Videos of Chain of Responsibility Design Pattern

Play Video