学习地址:http://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html
demo采用了DEBUG级别举例子,理解起来还是比较容易的,略做修改和总结:
责任链模式(Chain of responsibility Pattern
原理:责任链中所有的对象持有下一个状态对象的引用,若自己不满足条件,就交给下一个对象处理
java中应用:filter。。。
类图(晚上画):
代码:
1、创建抽象类,所有责任链对象继承本类:
package com.pat.chainresp;
/**
* 责任链模式
* @author Administrator
*
*/
public abstract class AbstractLogger {
public static int INFO=1;
public static int DEBUG=2;
public static int ERROR=3;
protected int level;
//下一个对象
protected AbstractLogger nextLogger;
//
public void setNextLogger(AbstractLogger nextLogger ){
this.nextLogger=nextLogger;
}
//责任传递
public void logMessage(int logLevel,String message) {
if(this.level==logLevel) {
write(message);
}
if(nextLogger!=null) {
nextLogger.logMessage(logLevel, message);
}
}
abstract protected void write(String message);
}
2、创建三个级别的子类,分别继承AbstractLogger
info级别:
package com.pat.chainresp;
public class InfoLogger extends AbstractLogger{
protected int level;
protected AbstractLogger nextLogger;
public void setNextLogger(AbstractLogger nextLogger ){
this.nextLogger=nextLogger;
}
//责任传递
public void logMessage(int logLevel,String message) {
if(this.level==logLevel) {
write(message);
}
if(nextLogger!=null) {
nextLogger.logMessage(logLevel, message);
}
}
@Override
protected void write(String message) {
System.out.println("InfoLog>>>"+message);
}
public InfoLogger(int level){
this.level=level;
}
}
debug级别:
package com.pat.chainresp;
public class DebugLogger extends AbstractLogger{
protected int level;
protected AbstractLogger nextLogger;
public void setNextLogger(AbstractLogger nextLogger ){
this.nextLogger=nextLogger;
}
//责任传递
public void logMessage(int logLevel,String message) {
if(this.level==logLevel) {
write(message);
}
if(nextLogger!=null) {
nextLogger.logMessage(logLevel, message);
}
}
@Override
protected void write(String message) {
System.out.println("DebugLogger>>>"+message);
}
public DebugLogger(int level){
this.level=level;
}
}
error级别:
package com.pat.chainresp;
public class ErrorLogger extends AbstractLogger{
protected int level;
protected AbstractLogger nextLogger;
public void setNextLogger(AbstractLogger nextLogger ){
this.nextLogger=nextLogger;
}
//责任传递
public void logMessage(int logLevel,String message) {
if(this.level==logLevel) {
write(message);
}
if(nextLogger!=null) {
nextLogger.logMessage(logLevel, message);
}
}
@Override
protected void write(String message) {
System.out.println("ErrorLogger>>>"+message);
}
public ErrorLogger(int level){
this.level=level;
}
}
3、组装责任链:
//组装责任链链条
public static AbstractLogger chainOfLevel(){
//创建三个日志级别的对象
AbstractLogger info = new InfoLogger(AbstractLogger.INFO);
AbstractLogger debug= new DebugLogger(AbstractLogger.DEBUG);
AbstractLogger error= new ErrorLogger(AbstractLogger.ERROR);
//设置责任链顺序
error.setNextLogger(debug);
debug.setNextLogger(info);
return error;
}
4、测试:
package com.pat.chainresp;
public class Test {
//组装责任链链条
public static AbstractLogger chainOfLevel(){
//创建三个日志级别的对象
AbstractLogger info = new InfoLogger(AbstractLogger.INFO);
AbstractLogger debug= new DebugLogger(AbstractLogger.DEBUG);
AbstractLogger error= new ErrorLogger(AbstractLogger.ERROR);
//设置责任链顺序
error.setNextLogger(debug);
debug.setNextLogger(info);
return error;
}
public static void main(String[] args) {
AbstractLogger chainCtrl = chainOfLevel();
chainCtrl.logMessage(AbstractLogger.INFO, " 日志级别info消息");
chainCtrl.logMessage(AbstractLogger.DEBUG, "日志级别debug 消息");
chainCtrl.logMessage(AbstractLogger.ERROR, "日志级别error 消息");
/*chainCtrl.logMessage(2, "debug 消息");
chainCtrl.logMessage(3, "err 消息");*/
}
}
5、结果:
InfoLog>>> 日志级别info消息
DebugLogger>>>日志级别debug 消息
ErrorLogger>>>日志级别error 消息