学习地址: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 消息

最新文章

  1. [Tomcat 源码分析系列] (一) : Tomcat 启动脚本-startup.bat
  2. JavaScript Patterns 5.4 Module Pattern
  3. iOS_隐藏顶部状态栏方式
  4. OC-设计模式KVC+KVO定义及使用
  5. 解决前端浏览器传JSON对像到服务端后全部变成string类型的方法
  6. windows内存管理方式以及优缺点
  7. Android OpenCV实现图片叠加,水印
  8. oracle与sqlserver区别
  9. LSTM模型与前向反向传播算法
  10. sublime text 3 打造舒适黑色主题
  11. 【专章】dp基础
  12. [转载] ZooKeeper实现分布式队列Queue
  13. Apache Atlas元数据管理从入门到实战(1)
  14. 基于WebGL架构的3D可视化平台—设备管理
  15. OPC转发阿里云alink工具
  16. [SCOI2010] 连续攻击游戏
  17. ASP.NET Core MVC中URL和数据模型的匹配
  18. Unity Alpha融合参数(便查)
  19. html2canvas - 微信中长按存图 - 将h5活动结果保存到本地
  20. asp.net mvc4连接mysql

热门文章

  1. PHP正则表达式入门教程[转]
  2. C# WPF 仿网易云音乐(PC)Banner动画控件
  3. 得知OpenCV研究报告指出系列(一)VS2010+OpenCV2.4.9环境配置
  4. Blend_Effect
  5. python3下的IE自动化模块PAMIE
  6. 开源库(要不要重新制造轮子)—— C/C++、Java、Python
  7. SequenceType 与 GeneratorType
  8. 用户控件(UserControl)
  9. Selenium-actions
  10. 关于WPF后台触发键盘按键