设计模式6--责任链模式(The chain of responsibility pattern)

来源:互联网 发布:软件开发网上兼职 编辑:程序博客网 时间:2024/05/22 06:22
如果一个处理不了,就传给链上的小一个来处理。
subject notification subject1

subject-->object1-->object2-->object3


interface HelpInterface{public void getHelp(int helpConstant);}public class FrontEnd implements HelpInterface{final int FRONT_END_HELP = 1;HelpInterface successor;public FrontEnd(HelpInterface s){successor = s;}public void getHelp(int helpConstant){if(helpConstant != FRONT_END_HELP) {successor.getHelp(helpConstant);} else {System.out.println("This is the front end.");}} }public class IntermediateLayer implements HelpInterface{final int INTERMEDIATE_LAYER_HELP = 2;HelpInterface successor;public FrontEnd(HelpInterface s){successor = s;}public void getHelp(int helpConstant){if(helpConstant != INTERMEDIATE_LAYER_HELP) {successor.getHelp(helpConstant);} else {System.out.println("This is the IntermediateLayer.");}} }public class Application implements HelpInterface{public Application(){}public void getHelp(int helpConstant){System.out.println("This is the MegaGigaCo application.");} }public class TestHelp{public static void main(String args[]){final int FRONT_END_HELP = 1;final int INTERMEDIATE_LAYER_HELP = 2;final int GENERAL_HELP = 3;Application app = new Application();IntermediateLayer intermediateLayer = new IntermediateLayer(app);FrontEnd frontEnd = new FrontEnd(intermediateLayer);frontEnd.getHelp(GENERAL_HELP);}}


0 0
原创粉丝点击