23种经典设计模式的java实现

对于职责链模式,一般是这么定义的:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,即——将这些对象连成一条(处理)链,并沿着这条链传递该请求,直到这条链结束同时有一个对象处理它为止。

最经典的职责链模式应用就是JavaEE规范中web.xml的filter了,filters按照配置的顺序,依次处理根据其mapping条件过滤得到的web请求(response)/应答(request),从而可以实现诸如编码转换、事务封装、页面缓存等众多“职责”。

给出职责链模式的适用范围:

1、有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。

2、想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。

3、可处理一个请求的对象集合需要被动态指定。

下面这个范例模拟了一个软件行业中常见的项目处理流程,为一个项目(Project对象)的职责人员构造了一条处理链,然后向这个项目发出处理指令——从而在这条链上引发相关职责人员的处理。代码如下:

职责的接口定义,所有的职责人员都要具有这些功能:加入下一职责对象、处理消息(即行使职责)、获得下一职责对象。

packagecom.alex.designpattern.chainofresponsibility;/***//***TheinterfaceofthechainYoucanuseAddChainfunctiontomodifythechain*dynamically.**@author<ahref="mailto:huqiyes@gmail.com">huqi</a>*@serialData2007*/publicinterfaceChain…{publicvoidaddChain(Chainc);publicvoidsendToChain(Stringmesg);publicChaingetChain();}

以下就是我们流程中所有的职责人员实现啦,依次为经理、项目经理、程序员、测试员及其它,它们根据mesg参数分别处理自己的消息——即行使特定的职责。

packagecom.alex.designpattern.chainofresponsibility;/***//***AbeginnerofthechainTheresposibilityofmanageristogetaproject*/publicclassManagerimplementsChain…{privateChainnextChain=null;privatestaticfinalStringResponsibility="GettingProject";publicvoidaddChain(Chainc)…{nextChain=c;}publicChaingetChain()…{returnnextChain;}publicvoidsendToChain(Stringmesg)…{if(mesg.equals(Responsibility))…{System.out.println("AManager–>"+mesg);}else…{if(nextChain!=null)…{nextChain.sendToChain(mesg);}}}}packagecom.alex.designpattern.chainofresponsibility;/***//***AmemberofthechainTheresposibilityofPMistodesigntheproject*/publicclassProjectManagerimplementsChain…{privateChainnextChain=null;privatestaticfinalStringResponsibility="Design";publicvoidaddChain(Chainc)…{nextChain=c;}publicChaingetChain()…{returnnextChain;}publicvoidsendToChain(Stringmesg)…{if(mesg.equals(Responsibility))…{System.out.println("AProjectManager–>"+mesg);}else…{if(nextChain!=null)…{nextChain.sendToChain(mesg);}}}}packagecom.alex.designpattern.chainofresponsibility;/***//***AmemberofthechainTheresposibilityofProgrammeriscoding*/publicclassProgrammerimplementsChain…{privateChainnextChain=null;privatestaticfinalStringResponsibility="Coding";publicvoidaddChain(Chainc)…{nextChain=c;}publicChaingetChain()…{returnnextChain;}publicvoidsendToChain(Stringmesg)…{if(mesg.equals(Responsibility))…{System.out.println("AProgrammer–>"+mesg);}else…{if(nextChain!=null)…{nextChain.sendToChain(mesg);}}}}packagecom.alex.designpattern.chainofresponsibility;/***//***Amemberofthechain<br>*TheresposibilityofQAistest*/publicclassQAimplementsChain…{privateChainnextChain=null;privatestaticfinalStringResponsibility="Testing";publicvoidaddChain(Chainc)…{nextChain=c;}publicChaingetChain()…{returnnextChain;}publicvoidsendToChain(Stringmesg)…{if(mesg.equals(Responsibility))…{System.out.println("AQA–>"+mesg);}else…{if(nextChain!=null)…{nextChain.sendToChain(mesg);}}}}packagecom.alex.designpattern.chainofresponsibility;/***//***Theendofthechain<br>*TheresposibilityofOthersishandleexeception*/publicclassOthersimplementsChain…{privateChainnextChain=null;publicvoidaddChain(Chainc)…{nextChain=c;}publicChaingetChain()…{returnnextChain;}publicvoidsendToChain(Stringmesg)…{System.out.println("Nonecanhandle–>"+mesg);}}

下面当然是测试的主程序了,首先我们按照顺序初始化这条“链”——项目来了,先是部门经理过目,然后交给相关项目经理做设计,有了设计程序员就可以编码了,完工后测试人员测试,最后来到职责链末尾结束(当然,真实的情况下,不会有这么顺利的项目:-)。注意我们在这里定义了一个Project类,将由它的对象引发这条“链”的连锁反应。

packagecom.alex.designpattern.chainofresponsibility;/***//***职责链模式*<p>*使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。<br>*将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。**@author<ahref="mailto:huqiyes@gmail.com">huqi</a>*@serialData2007*/publicclassTest…{publicstaticvoidmain(String[]args)…{Projectproject=newProject();ManageraManager=newManager();ProjectManageraPM=newProjectManager();ProgrammeraProgrammer=newProgrammer();QAaQA=newQA();Othersothers=newOthers();//initchainaManager.addChain(aPM);aPM.addChain(aProgrammer);aProgrammer.addChain(aQA);aQA.addChain(others);project.setChain(aManager);project.getChain().sendToChain("GettingProject");project.getChain().sendToChain("Design");project.getChain().sendToChain("Coding");project.getChain().sendToChain("Testing");project.getChain().sendToChain("Over");}}classProject…{privateChainchain;publicChaingetChain()…{returnchain;}publicvoidsetChain(Chainchain)…{this.chain=chain;}}

结合这个例子,那就很容易理解web.xml中常用的filter了吧,每个filter都是职责链中的一环,它们按照自己的职责过滤web的请求、应答……这就是职责链模式。

仿佛松树就是一位威风的将军,守护着国家的国民。

23种经典设计模式的java实现

相关文章:

你感兴趣的文章:

标签云: