23种经典设计模式的java实现_5_职责链模式

本文深入解析职责链模式,通过web.xml中filter的实际应用案例,详细阐述了职责链模式的适用范围、核心原理及代码实现,旨在帮助开发者理解和掌握这一设计模式在实际开发中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转自:http://blog.youkuaiyun.com/kthq/article/details/1713720

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

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

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

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

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

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

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

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

package com.alex.designpattern.chainofresponsibility;

/**
*TheinterfaceofthechainYoucanuseAddChainfunctiontomodifythechain
*dynamically.
*
*
@author<ahref="mailto:huqiyes@gmail.com">huqi</a>
*
@serialData2007
*/

public interface Chain {
publicvoidaddChain(Chainc);

publicvoidsendToChain(Stringmesg);

publicChaingetChain();
}

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

package com.alex.designpattern.chainofresponsibility;

/**
*AbeginnerofthechainTheresposibilityofmanageristogetaproject
*/

public class Manager implements Chain {
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);
}

}

}


}


package com.alex.designpattern.chainofresponsibility;

/**
*AmemberofthechainTheresposibilityofPMistodesigntheproject
*/

public class ProjectManager implements Chain {
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);
}

}

}


}


package com.alex.designpattern.chainofresponsibility;

/**
*AmemberofthechainTheresposibilityofProgrammeriscoding
*/

public class Programmer implements Chain {
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);
}

}

}


}


package com.alex.designpattern.chainofresponsibility;

/**
*Amemberofthechain<br>
*TheresposibilityofQAistest
*/

public class QA implements Chain {
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);
}

}

}

}


package com.alex.designpattern.chainofresponsibility;

/**
*Theendofthechain<br>
*TheresposibilityofOthersishandleexeception
*/

public class Others implements Chain {
privateChainnextChain=null;

publicvoidaddChain(Chainc){
nextChain
=c;
}


publicChaingetChain(){
returnnextChain;
}


publicvoidsendToChain(Stringmesg){
System.out.println(
"Nonecanhandle-->"+mesg);
}


}

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

package com.alex.designpattern.chainofresponsibility;

/**
*职责链模式
*<p>
*使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。<br>
*将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
*
*
@author<ahref="mailto:huqiyes@gmail.com">huqi</a>
*
@serialData2007
*/

public class Test {
publicstaticvoidmain(String[]args){

Projectproject
=newProject();

ManageraManager
=newManager();
ProjectManageraPM
=newProjectManager();
ProgrammeraProgrammer
=newProgrammer();
QAaQA
=newQA();
Othersothers
=newOthers();
//initchain
aManager.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");
}

}


class Project {
privateChainchain;

publicChaingetChain(){
returnchain;
}


publicvoidsetChain(Chainchain){
this.chain=chain;
}

}

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值