# Chain of Responsibility

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Chain of Responsibility (which is a Structural Pattern).

# Chain of Responsibility Pattern - theory

Chain Of Responsibility Design Pattern in Java is used when we want to have a hierarchy of subsequent calls. One request from a client is passed to a chain of objects to process them.

Take a look at the following UML diagram representing the Chain Of Responsibility design pattern (for my example):

# Chain of Responsibility Pattern - example

Here is an example of using the Chain of Responsibility Design Pattern in Java:

package chainofresponsability.java.pattern.example;
 
public abstract class ChainElement {
  
       // the level of importance 0=MAXIMUM, 1=MEDIUM, 2=LOW 
       protected int level;
     
       //next element in the Chain of Responsibility
       protected ChainElement nextChainElement;
 
       public void setNextChainElement(ChainElement nextChainElement){
          this.nextChainElement = nextChainElement;
       }
 
       public void tryToSendEmail(int level, String message){
           
           if(this.level >= level){
              sendEmail(message);
          }
           
          if(nextChainElement !=null){
              nextChainElement.tryToSendEmail(level, message);
          }
       }
 
       abstract protected void sendEmail(String message);
         
}
package chainofresponsability.java.pattern.example;
 
public class Analyst extends ChainElement{
 
    public Analyst (int level){
          this.level = level;
       }
     
    protected void sendEmail(String message){
        System.out.println("\nAn email was send to the ANALYSTS. The message was:");
        System.out.println(message);
         
    }
     
}
package chainofresponsability.java.pattern.example;
 
public class ProjectManager extends ChainElement{
 
    public ProjectManager (int level){
          this.level = level;
       }
     
    protected void sendEmail(String message){
        System.out.println("\nAn email was send to the PROJECT MANAGER. The message was:");
        System.out.println(message);
    }
}
package chainofresponsability.java.pattern.example;
 
public class Director extends ChainElement{
 
    public Director (int level){
          this.level = level;
       }
     
    protected void sendEmail(String message){
        System.out.println("\nAn email was send to the DIRECTOR. The message was:");
        System.out.println(message);
    }
}
package chainofresponsability.java.pattern.example;
 
public class ChainOfResponsabilityJavaExample {
 
    public static void main(String[] args) {
         
       ChainElement firstChainElement = new Analyst(2);
       ChainElement secondChainElement = new ProjectManager(1);
       ChainElement thirdChainElement = new Director(0);
     
       firstChainElement.setNextChainElement(secondChainElement);
       secondChainElement.setNextChainElement(thirdChainElement);
        
       firstChainElement.tryToSendEmail(0, "This is a HIGH priority message. START from 1st Chain element"); 
       System.out.println("----------------------------------------------------------------");
        
       firstChainElement.tryToSendEmail(1, "This is a MEDIUM priority message. START from 1st Chain element");
       System.out.println("----------------------------------------------------------------");
        
       thirdChainElement.tryToSendEmail(0, "This is a HIGH priority message. START from 3rd Chain element");
       System.out.println("----------------------------------------------------------------");
    }
}

When you run this example you will receive the following result: