【设计模式】行为型05责任链模式(Chain of responsibility Pattern)
学习地址: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)的更多相关文章
- 乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern)
原文:乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 责任链模式(Chain of R ...
- 二十四种设计模式:责任链模式(Chain of Responsibility Pattern)
责任链模式(Chain of Responsibility Pattern) 介绍为解除请求的发送者和接收者之间耦合,而使多个对象都有机会处理这个请求.将这些对象连成一条链,并沿着这条链传递该请求,直 ...
- C#设计模式-责任链模式(Chain of Responsibility Pattern)
引子 一个事件需要经过多个对象处理是一个挺常见的场景,譬如采购审批流程,请假流程,软件开发中的异常处理流程,web请求处理流程等各种各样的流程,可以考虑使用责任链模式来实现.现在以请假流程为例,一般公 ...
- 23种设计模式--责任链模式-Chain of Responsibility Pattern
一.责任链模式的介绍 责任链模式用简单点的话来说,将责任一步一步传下去,这就是责任,想到这个我们可以相当击鼓传花,这个是为了方便记忆,另外就是我们在项目中经常用到的审批流程等这一类的场景时我们就可以考 ...
- 责任链模式 职责链模式 Chain of Responsibility Pattern 行为型 设计模式(十七)
责任链模式(Chain of Responsibility Pattern) 职责链模式 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系 将这些对象连接成一条链,并沿着这 ...
- atitit.设计模式(1)--—职责链模式(chain of responsibility)最佳实践O7 日期转换
atitit.设计模式(1)---职责链模式(chain of responsibility)最佳实践O7 日期转换 1. 需求:::日期转换 1 2. 可以选择的模式: 表格模式,责任链模式 1 3 ...
- 责任链模式-Chain of Responsibility(Java实现), 例1
责任链模式-Chain of Responsibility, 例1 在这种模式中,通常每个接收者都包含对另一个接收者的引用.如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推. ...
- 责任链模式-Chain of Responsibility(Java实现), 例2
责任链模式-Chain of Responsibility 在这种模式中,通常每个接收者都包含对另一个接收者的引用.如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推. 咱们在 ...
- 责任链模式/chain of responsibility/行为型模式
职责链模式 chain of responsibility 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处 ...
- 《JAVA设计模式》之责任链模式(Chain of Responsibility)
在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其 ...
随机推荐
- WPF中取得预定义颜色
原文:WPF中取得预定义颜色 使用XAML代码取得.net预定义颜色:<Page xmlns="http://schemas.microsoft.com/winfx/2006/x ...
- Global Contrast based Salient Region Detection (Ming ming Cheng)
abstract: Automatic estimation of salient object regions across images, without any prior assumption ...
- cocos2d-x 3.2 它 2048 —— 第三
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- TASM 5.0 安装及使用教程
安装TASM 5.0很简单,您只需要下载本站[相关工具]中的"TASM50.zip"文件,解压后在Windows9x/NT下执行"INSTALL.EXE"即可开 ...
- 投资人的能量往往大多远远不仅于此,他能站在不同的角度和高度看问题(要早点拿投资,要舍得让出股份)——最好不要让 Leader 一边做技术、一边做管理,人的能力是有限的,精力也是有限的
摘要:在创业三年时间里作为联合创始人,虽然拿着大家均等的股份,我始终是没有什么话语权的,但是,这也给了我从旁观者的角度看清整个局面的机会.创业公司的成败绝大程度取决于技术大牛和公司 Leader, ...
- ADB 基础命令使用
1.adb shell(>=2个设备显示:error: more than one device/emulator,仅连接一个设备可用) adb -d shell 只运行在真实设备中 adb - ...
- Qt之自定义搜索框——QLineEdit里增加一个Layout,还不影响正常输入文字(好像是一种比较通吃的方法)
简述 关于搜索框,大家都经常接触.例如:浏览器搜索.Windows资源管理器搜索等. 当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定. 方案一:调用QLineEdit现有接口 void ...
- CROSS JOIN
原文:CROSS JOIN 最近在讲到T-SQL查询的Join部分时,一下子没有想起来CROSS JOIN的用法,因为其实平常也确实基本不用到.特意找了一个例子,以供参考 CROSS JOIN又称为笛 ...
- 想让一个Widget成为模态,我们只需要对其设置setAttribute(Qt::WA_ShowModal, true);
想让一个Widget成为模态,我们只需要对其设置: setAttribute(Qt::WA_ShowModal, true); 注意:这是QWidget的成员函数 ,也就是说,QWidget可以显示为 ...
- 可以用GetObjectProp来获取对象的属性
原来可以用GetObjectProp来获取对象的属性,还有这用法,哈哈哈哈…… var SL: TStrings; UseDBTools: Boolean;begin SL := nil; if Me ...