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

【设计模式】行为型05责任链模式(Chain of responsibility Pattern)的更多相关文章

  1. 乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern)

    原文:乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 责任链模式(Chain of R ...

  2. 二十四种设计模式:责任链模式(Chain of Responsibility Pattern)

    责任链模式(Chain of Responsibility Pattern) 介绍为解除请求的发送者和接收者之间耦合,而使多个对象都有机会处理这个请求.将这些对象连成一条链,并沿着这条链传递该请求,直 ...

  3. C#设计模式-责任链模式(Chain of Responsibility Pattern)

    引子 一个事件需要经过多个对象处理是一个挺常见的场景,譬如采购审批流程,请假流程,软件开发中的异常处理流程,web请求处理流程等各种各样的流程,可以考虑使用责任链模式来实现.现在以请假流程为例,一般公 ...

  4. 23种设计模式--责任链模式-Chain of Responsibility Pattern

    一.责任链模式的介绍 责任链模式用简单点的话来说,将责任一步一步传下去,这就是责任,想到这个我们可以相当击鼓传花,这个是为了方便记忆,另外就是我们在项目中经常用到的审批流程等这一类的场景时我们就可以考 ...

  5. 责任链模式 职责链模式 Chain of Responsibility Pattern 行为型 设计模式(十七)

    责任链模式(Chain of Responsibility Pattern) 职责链模式 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系 将这些对象连接成一条链,并沿着这 ...

  6. atitit.设计模式(1)--—职责链模式(chain of responsibility)最佳实践O7 日期转换

    atitit.设计模式(1)---职责链模式(chain of responsibility)最佳实践O7 日期转换 1. 需求:::日期转换 1 2. 可以选择的模式: 表格模式,责任链模式 1 3 ...

  7. 责任链模式-Chain of Responsibility(Java实现), 例1

    责任链模式-Chain of Responsibility, 例1 在这种模式中,通常每个接收者都包含对另一个接收者的引用.如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推. ...

  8. 责任链模式-Chain of Responsibility(Java实现), 例2

    责任链模式-Chain of Responsibility 在这种模式中,通常每个接收者都包含对另一个接收者的引用.如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推. 咱们在 ...

  9. 责任链模式/chain of responsibility/行为型模式

    职责链模式 chain of responsibility 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处 ...

  10. 《JAVA设计模式》之责任链模式(Chain of Responsibility)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其 ...

随机推荐

  1. Python 函数调用性能记录

    之前用 JS 写项目的时候,项目组用的组件模式,一直感觉很不错.最近用 Python 做新项目,项目结构也延续了组件模式.一直没有对函数调用的性能作了解,今天突发奇想测试了一下,写了一些测试代码 首先 ...

  2. Matlab Tricks(十九)—— 序列左右移的实现

    比如实现如下的移位操作: y(n)=x(n−k) function [y, n] = sigshift(x, m, k) n = m + k; y = x; 本身任意一个 matlab 序列本质上都是 ...

  3. WPF中Polyline拐角的bug

    原文:WPF中Polyline拐角的bug       Polyline绘制折线在小角度(比如几度)的时候会出现不连续的现象,形成拐角的两条线段中有一段会超出,角度越小越明显.       问题如下图 ...

  4. c#-WPF string,color,brush之间的转换

    原文:c#-WPF string,color,brush之间的转换 String转换成Color string-"ffffff" Color color = (Color)Colo ...

  5. 王立平--RemoteView

    RemoteView它将在两个地方被使用:一个是在AppWidget,另外一个是在Notification. RemoteView是用来描写叙述一个垮进程显示的view 1.AppWidget---R ...

  6. OnNavigatedTo 和 Loaded 的比较

    直接上结果: OnNavigateTo :是在导航完成,在控件或者页面加载前(之间)调用. Loaded :是在页面准备好并且在控件加载完成后调用. 参考资料: 1.https://stackover ...

  7. .net core config读取

    最简单的方式 引用 Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.Json json文件 新建一个Conf ...

  8. 通通玩blend美工(6)上——仿iPhone滚动选择器的ListBox(UI设计)

    原文:通通玩blend美工(6)上--仿iPhone滚动选择器的ListBox(UI设计) 好久没更新博客了,由于项目比较紧,期间收到不少园友的短消息,感谢大家对我的支持~~. 相信各位都在自己的神机 ...

  9. Cordova页面加载外网图片失败,Refused to load the image

    原文:Cordova页面加载外网图片失败,Refused to load the image 1.使用Cordova页面加载外网图片失败,抛出异常 Refused to load the image ...

  10. Assertion failure in UITableViewCell layoutSublayersOfLayer解决办法

    iOS6 设备在更新UITableViewCell的时候遇到了 Assertion failure in -[UITableViewCell layoutSublayersOfLayer:], /So ...