Saturday, June 12, 2021

Chain Of Responsibility Design Pattern in JAVA

Why you would choose the chain of responsibility pattern?

Because they decouple the sender and receiver objects often times in an application. 
We want to pass a request to a receiving object without knowing who the sender was and vice versa. The sender shouldn't have to know who the receiver was in order for to process that request. When using the chain, the receiver should also contain a reference to the next receiver or the successor. This is an important part when choosing and implementing this pattern.

 It doesn't know the whole hierarchy, but it does know who's next in line. One of the main reasons for choosing this pattern is to promote loose coupling. 

We can modify the chain and add links to the chain without rewriting large portions of logic in the application. It should also be okay that there may not be a handler for a given request, and the application will just continue on examples of this in the job. For example how spring implements their security chain filter in spring security.

I will give two example one JAVA API reference and We will create one for us with real time example.

First lets have a look into the UML Diagram for the same.

UML Diagram:





Lets understand why we should consider this design pattern as you have seen the UML diagram.

the design of the chain Responsibility has a chain of receiver objects. This can be implemented in a number of ways, but some basic form of a list is typically the most common. Each handler is based off of a main interface that defines the contract between each chain link. There is a concrete handler for each receiver or implementation that will interpret a request in building the chain. Each handler has a reference to its successor or the next link in the chain. The pieces of the UML diagram are a handler and a concrete handler and then its successor.

Lets look into the code:

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ChainOfresponsibility
{
private static final Logger logger = Logger.getLogger(ChainOfresponsibility.class.getName());
public static void main(String[] args)
{
logger.setLevel(Level.FINER);
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.FINER);
logger.addHandler(handler);
logger.finest("Finest");
logger.finer("Finer");
logger.fine("Fine");
}
}


Output:

Jun 12, 2021 3:30:09 PM ChainOfresponsibility main
FINER: Finer
Jun 12, 2021 3:30:09 PM ChainOfresponsibility main

For Full source code go to my GitHub URL mentioned below:

https://github.com/Abhinaw/DesignPatternInJava/tree/master/src/chainofresponsibility
Here you will get all the required files for complete source code.



Let me explain the example that i took to implement this design pattern.
we have a hierarchy in our company such as CEO -> VP -> Director right?
So Director have some specific limited rights and VP have more rights as compared to Director and obviously, CEO have all the rights he can do anything he wants. here we have chain of responsibility.
More certainly, lets take example that Director can approve a purchase of Rs,1000 only and if it is more than Rs,1000 then it got to VP for approval and if it more than that it should go to CEO for the approval.

public class ChainOfResponsibility {

    public static void main(String[] args) {
        Director director = new Director();
        VP vp = new VP();
        CEO ceo = new CEO();

        director.setSuccessor(vp);
        vp.setSuccessor(ceo);
        Request request = new Request(RequestType.CONFERENCE, 500);
        director.handleRequest(request);

        request = new Request(RequestType.PURCHASE, 1000);
        director.handleRequest(request);

        request = new Request(RequestType.PURCHASE, 2000);
        director.handleRequest(request);
    }


FINE: Fine






2 comments:

Anonymous said...

Very nice example.thank you.

Abhinaw Tripathi said...

Thank you