Chain of responsibility pattern

In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain. Thus, the chain of responsibility is an object oriented version of the if ... else if ... else if ....... else ... end if idiom, with the benefit that the condition–action blocks can be dynamically rearranged and reconfigured at runtime.

[Wikipedia]

Intro

In this example we will simulate a costumer service system , which gets problems and according to the diffcult of the problem, the matching employ will solve it. If a low level employee sees that the problem is too hard for him, he delegate it to the upper level, and so on. 

Chain


public interface Chain {
    public void setNextChain(Chain higherLevel);
    public void solveTheProblem(String levelOfProblem);
}

Low Level Employee


  public class LowLevelEmployee implements Chain {
    private Chain nextChain;
    @Override
    public void setNextChain(Chain higherLevel) {
        this.nextChain=higherLevel;
    }

    @Override
    public void solveTheProblem(String levelOfProblem) {
        if(levelOfProblem.compareTo("LowLevelEmployee")==0)
            System.out.println("Low Level Employee has solved the problem!");
        else
        {
            System.out.println("This problem is above Low Level Emplotee seniority. You are being transferred to the higher rank...");
            nextChain.solveTheProblem(levelOfProblem);
        }
    }
}

Medium Level Employee


  public class MediumLevelEmployee implements Chain {
    private Chain nextChain;
    @Override
    public void setNextChain(Chain higherLevel) {
        this.nextChain=higherLevel;
    }

    @Override
    public void solveTheProblem(String levelOfProblem) {
        if(levelOfProblem.compareTo("MediumLevelEmployee")==0)
            System.out.println("Medium Level Employee has solved the problem!");
        else
        {
            System.out.println("This problem is above Medium Level Employee seniority. You are being transferred to the higher rank...");
            nextChain.solveTheProblem(levelOfProblem);
        }
    }
}

Team Leader


  public class TeamLeader implements Chain {
    private Chain nextChain;
    @Override
    public void setNextChain(Chain higherLevel) {
        this.nextChain=higherLevel;
    }

    @Override
    public void solveTheProblem(String levelOfProblem) {
        if(levelOfProblem.compareTo("TeamLeader")==0)
            System.out.println("Team Leader has solved the problem!");
        else
        {
            System.out.println("This problem is above Team Leader seniority. You are being transferred to the higher rank...");
            nextChain.solveTheProblem(levelOfProblem);
        }
    }
}

CTO


  public class CTO implements Chain {
    private Chain nextChain;
    @Override
    public void setNextChain(Chain higherLevel) {
        this.nextChain=higherLevel;
    }

    @Override
    public void solveTheProblem(String levelOfProblem) {
        if(levelOfProblem.compareTo("CTO")==0)
            System.out.println("CTO has solved the problem!");
        else
            System.out.println("This problem is above CTO seniority. Find the solution yourself.");

    }
}

Main


    public class Main {
        
        /**
        We will simulate a costumer service system , which gets problems and according to the diffcult of the problem,
        the matching employ will solve it.
        If a low level employee sees that the problem is too hard for him, he delegate it to the upper level, and so on.
         */

      public static void main(String []args) {
        LowLevelEmployee emp1 = new LowLevelEmployee();
        MediumLevelEmployee emp2 = new MediumLevelEmployee();
        TeamLeader emp3 = new TeamLeader();
        CTO emp4 = new CTO();

        emp1.setNextChain(emp2);
        emp2.setNextChain(emp3);
        emp3.setNextChain(emp4);

        System.out.println("CTO problem: ");
        emp1.solveTheProblem("CTO");
        System.out.println("----------------------------------------");
        System.out.println("Low Level Employee problem: ");
        emp1.solveTheProblem("LowLevelEmployee");
        System.out.println("----------------------------------------");
        System.out.println("Team Leader problem: ");
        emp1.solveTheProblem("TeamLeader");
        System.out.println("----------------------------------------");
        System.out.println("World CEO problem: ");
        emp1.solveTheProblem("World CEO");
        System.out.println("----------------------------------------");

    }
}

Running Example


CTO problem: 
This problem is above Low Level Emplotee seniority. You are being transferred to the higher rank...
This problem is above Medium Level Employee seniority. You are being transferred to the higher rank...
This problem is above Team Leader seniority. You are being transferred to the higher rank...
CTO has solved the problem!
----------------------------------------
Low Level Employee problem: 
Low Level Employee has solved the problem!
----------------------------------------
Team Leader problem: 
This problem is above Low Level Emplotee seniority. You are being transferred to the higher rank...
This problem is above Medium Level Employee seniority. You are being transferred to the higher rank...
Team Leader has solved the problem!
----------------------------------------
World CEO problem: 
This problem is above Low Level Emplotee seniority. You are being transferred to the higher rank...
This problem is above Medium Level Employee seniority. You are being transferred to the higher rank...
This problem is above Team Leader seniority. You are being transferred to the higher rank...
This problem is above CTO seniority. Find the solution yourself.
----------------------------------------