23种经典设计模式的java实现_5_职责链模式
Posted AlexanderHu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了23种经典设计模式的java实现_5_职责链模式相关的知识,希望对你有一定的参考价值。
对于职责链模式,一般是这么定义的:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,即——将这些对象连成一条(处理)链,并沿着这条链传递该请求,直到这条链结束同时有一个对象处理它为止。
最经典的职责链模式应用就是JavaEE规范中web.xml的filter了,filters按照配置的顺序,依次处理根据其mapping条件过滤得到的web请求(response)/应答(request),从而可以实现诸如编码转换、事务封装、页面缓存等众多“职责”。
给出职责链模式的适用范围:
1、有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。
2、想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
3、可处理一个请求的对象集合需要被动态指定。
下面这个范例模拟了一个软件行业中常见的项目处理流程,为一个项目(Project对象)的职责人员构造了一条处理链,然后向这个项目发出处理指令——从而在这条链上引发相关职责人员的处理。代码如下:
职责的接口定义,所有的职责人员都要具有这些功能:加入下一职责对象、处理消息(即行使职责)、获得下一职责对象。
package
com.alex.designpattern.chainofresponsibility;
/** */ /**
* The interface of the chain You can use AddChain function to modify the chain
* dynamically.
*
* @author <a href="mailto:huqiyes@gmail.com">huqi</a>
* @serialData 2007
*/
public interface Chain ... {
public void addChain(Chain c);
public void sendToChain(String mesg);
public Chain getChain();
}
/** */ /**
* The interface of the chain You can use AddChain function to modify the chain
* dynamically.
*
* @author <a href="mailto:huqiyes@gmail.com">huqi</a>
* @serialData 2007
*/
public interface Chain ... {
public void addChain(Chain c);
public void sendToChain(String mesg);
public Chain getChain();
}
以下就是我们流程中所有的职责人员实现啦,依次为经理、项目经理、程序员、测试员及其它,它们根据mesg参数分别处理自己的消息——即行使特定的职责。
package
com.alex.designpattern.chainofresponsibility;
/** */ /**
* A beginner of the chain The resposibility of manager is to get a project
*/
public class Manager implements Chain ... {
private Chain nextChain = null;
private static final String Responsibility = "Getting Project";
public void addChain(Chain c) ...{
nextChain = c;
}
public Chain getChain() ...{
return nextChain;
}
public void sendToChain(String mesg) ...{
if (mesg.equals(Responsibility)) ...{
System.out.println("A Manager --> " + mesg);
} else ...{
if (nextChain != null) ...{
nextChain.sendToChain(mesg);
}
}
}
}
package com.alex.designpattern.chainofresponsibility;
/** */ /**
* A member of the chain The resposibility of PM is to design the project
*/
public class ProjectManager implements Chain ... {
private Chain nextChain = null;
private static final String Responsibility = "Design";
public void addChain(Chain c) ...{
nextChain = c;
}
public Chain getChain() ...{
return nextChain;
}
public void sendToChain(String mesg) ...{
if (mesg.equals(Responsibility)) ...{
System.out.println("A Project Manager --> " + mesg);
} else ...{
if (nextChain != null) ...{
nextChain.sendToChain(mesg);
}
}
}
}
package com.alex.designpattern.chainofresponsibility;
/** */ /**
* A member of the chain The resposibility of Programmer is coding
*/
public class Programmer implements Chain ... {
private Chain nextChain = null;
private static final String Responsibility = "Coding";
public void addChain(Chain c) ...{
nextChain = c;
}
public Chain getChain() ...{
return nextChain;
}
public void sendToChain(String mesg) ...{
if (mesg.equals(Responsibility)) ...{
System.out.println("A Programmer --> " + mesg);
} else ...{
if (nextChain != null) ...{
nextChain.sendToChain(mesg);
}
}
}
}
package com.alex.designpattern.chainofresponsibility;
/** */ /**
* A member of the chain<br>
* The resposibility of QA is test
*/
public class QA implements Chain ... {
private Chain nextChain = null;
private static final String Responsibility = "Testing";
public void addChain(Chain c) ...{
nextChain = c;
}
public Chain getChain() ...{
return nextChain;
}
public void sendToChain(String mesg) ...{
if (mesg.equals(Responsibility)) ...{
System.out.println("A QA --> " + mesg);
/** */ /**
* A beginner of the chain The resposibility of manager is to get a project
*/
public class Manager implements Chain ... {
private Chain nextChain = null;
private static final String Responsibility = "Getting Project";
public void addChain(Chain c) ...{
nextChain = c;
}
public Chain getChain() ...{
return nextChain;
}
public void sendToChain(String mesg) ...{
if (mesg.equals(Responsibility)) ...{
System.out.println("A Manager --> " + mesg);
} else ...{
if (nextChain != null) ...{
nextChain.sendToChain(mesg);
}
}
}
}
package com.alex.designpattern.chainofresponsibility;
/** */ /**
* A member of the chain The resposibility of PM is to design the project
*/
public class ProjectManager implements Chain ... {
private Chain nextChain = null;
private static final String Responsibility = "Design";
public void addChain(Chain c) ...{
nextChain = c;
}
public Chain getChain() ...{
return nextChain;
}
public void sendToChain(String mesg) ...{
if (mesg.equals(Responsibility)) ...{
System.out.println("A Project Manager --> " + mesg);
} else ...{
if (nextChain != null) ...{
nextChain.sendToChain(mesg);
}
}
}
}
package com.alex.designpattern.chainofresponsibility;
/** */ /**
* A member of the chain The resposibility of Programmer is coding
*/
public class Programmer implements Chain ... {
private Chain nextChain = null;
private static final String Responsibility = "Coding";
public void addChain(Chain c) ...{
nextChain = c;
}
public Chain getChain() ...{
return nextChain;
}
public void sendToChain(String mesg) ...{
if (mesg.equals(Responsibility)) ...{
System.out.println("A Programmer --> " + mesg);
} else ...{
if (nextChain != null) ...{
nextChain.sendToChain(mesg);
}
}
}
}
package com.alex.designpattern.chainofresponsibility;
/** */ /**
* A member of the chain<br>
* The resposibility of QA is test
*/
public class QA implements Chain ... {
private Chain nextChain = null;
private static final String Responsibility = "Testing";
public void addChain(Chain c) ...{
nextChain = c;
}
public Chain getChain() ...{
return nextChain;
}
public void sendToChain(String mesg) ...{
if (mesg.equals(Responsibility)) ...{
System.out.println("A QA --> " + mesg);
以上是关于23种经典设计模式的java实现_5_职责链模式的主要内容,如果未能解决你的问题,请参考以下文章