【2013-12-17】设计模式学习笔记:责任链模式 📅 2026/7/15 6:06:21 [历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2013-12-17 标题设计模式学习笔记责任链模式分类编程 / 设计模式 / java 标签设计模式·java·责任链模式设计模式学习笔记责任链模式最近在看《设计模式之禅》这本书收获良多作者不愧是工作多年的大牛将各种设计模式讲解的非常透彻。这里备份下书中的【 责任链模式 】代码github【 https://github.com/cstriker1407/design_pattern 】责任链基类abstractclassChainHandler{privateintmaxNum;privateChainHandlernext;protectedChainHandler(intmaxNum){super();this.maxNummaxNum;}publicvoidsetNext(ChainHandlernext){this.nextnext;}protectedabstractvoidshowInfo(inttarget);publicfinalvoidprocess(inttarget){if(targetmaxNum){showInfo(target);}else{if(next!null){next.process(target);}else{System.out.println(NO handler);}}}}各级责任链classChainHandler0extendsChainHandler{publicChainHandler0(){super(0);}OverrideprotectedvoidshowInfo(inttarget){System.out.println(Handler0 hanler-target);}}classChainHandler1extendsChainHandler{publicChainHandler1(){super(1);}OverrideprotectedvoidshowInfo(inttarget){System.out.println(Handler1 hanler-target);}}classChainHandler2extendsChainHandler{publicChainHandler2(){super(2);}OverrideprotectedvoidshowInfo(inttarget){System.out.println(Handler2 hanler-target);}}测试代码publicclassResponsechainTest{publicstaticvoidtest(){ChainHandler0c0newChainHandler0();ChainHandler1c1newChainHandler1();ChainHandler2c2newChainHandler2();c0.setNext(c1);c1.setNext(c2);c0.process(0);c0.process(1);c0.process(2);c0.process(3);}}