Watch What is Going On with the Chain of Responsibility Patterns : Chain of Responsibility Patterns « Design Pattern « Java Tutorial






public class MainClass {
  public static void main(String args[]) {

    Application app = new Application();

    IntermediateLayer intermediateLayer = new IntermediateLayer(app);

    FrontEnd frontEnd = new FrontEnd(intermediateLayer);

    frontEnd.getHelp(HelpType.GENERAL_HELP);
  }
}

class HelpType {
  public final static int FRONT_END_HELP = 1;

  public final static int INTERMEDIATE_LAYER_HELP = 2;

  public final static int GENERAL_HELP = 3;
}

interface HelpInterface {
  public void getHelp(int helpConstant);
}

class IntermediateLayer implements HelpInterface {
  HelpInterface successor;

  public IntermediateLayer(HelpInterface s) {
    successor = s;
  }

  public void getHelp(int helpConstant) {
    if (helpConstant != HelpType.INTERMEDIATE_LAYER_HELP) {
      successor.getHelp(helpConstant);
    } else {
      System.out.println("intermediate");
    }
  }
}

class FrontEnd implements HelpInterface {
  HelpInterface successor;

  public FrontEnd(HelpInterface s) {
    successor = s;
  }

  public void getHelp(int helpConstant) {
    if (helpConstant != HelpType.FRONT_END_HELP) {
      successor.getHelp(helpConstant);
    } else {
      System.out.println("front end");
    }
  }
}

class Application implements HelpInterface {
  public void getHelp(int helpConstant) {
    System.out.println("application");
  }
}








34.7.Chain of Responsibility Patterns
34.7.1.Watch What is Going On with the Chain of Responsibility Patterns
34.7.2.Chain of responsibility
34.7.3.Multiple dispatching