设计模式之责任链模式

1 责任链模式

责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。

2 优点和缺点

职责链上的处理者负责处理请求,客户只需要将请求发送到职责链上即可,无须关心请求的处理细节和请求的传递,所以职责链将请求的发送者和请求的处理者解耦了。

3 测试Demo类图

我们创建抽象类 AbstractLogger,带有详细的日志记录级别。然后我们创建三种类型的记录器,都扩展了 AbstractLogger。每个记录器消息的级别是否属于自己的级别,如果是则相应地打印出来,否则将不打印并把消息传给下一个记录器

4 代码实现

1) AbstractLogger.java

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 level, String message){
      if(this.level <= level){
         write(message);
      }
      if(nextLogger !=null){
         nextLogger.logMessage(level, message);
      }
   }

   abstract protected void write(String message);

}

2)ConsoleLogger.java

public class ConsoleLogger extends AbstractLogger {

   public ConsoleLogger(int level){
      this.level = level;
   }

   @Override
   protected void write(String message) {
      System.out.println("Standard Console::Logger: " + message);
   }
}

3)ErrorLogger.java

public class ErrorLogger extends AbstractLogger {

   public ErrorLogger(int level){
      this.level = level;
   }

   @Override
   protected void write(String message) {
      System.out.println("Error Console::Logger: " + message);
   }
}

4)FileLogger.java

public class FileLogger extends AbstractLogger {

   public FileLogger(int level){
      this.level = level;
   }

   @Override
   protected void write(String message) {
      System.out.println("File::Logger: " + message);
   }
}

5) ChainPatternDemo.java

public class ChainPatternDemo {

   private static AbstractLogger getChainOfLoggers(){

      AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
      AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);
      AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);

      errorLogger.setNextLogger(fileLogger);
      fileLogger.setNextLogger(consoleLogger);

      return errorLogger;
   }

   public static void main(String[] args) {
      AbstractLogger loggerChain = getChainOfLoggers();

      loggerChain.logMessage(AbstractLogger.INFO, "This is an information.");

      loggerChain.logMessage(AbstractLogger.DEBUG,
         "This is a debug level information.");

      loggerChain.logMessage(AbstractLogger.ERROR,
         "This is an error information.");
   }
}

5 运行结果

Standard Console::Logger: This is an information.
File::Logger: This is a debug level information.
Standard Console::Logger: This is a debug level information.
Error Console::Logger: This is an error information.
File::Logger: This is an error information.
Standard Console::Logger: This is an error information.

参考地址:https://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html

(0)

相关推荐

  • winston log 库如何创建 custom logger

    代码: const { createLogger, format, transports, config } = require('winston'); const usersLogger = cre ...

  • Python 日志打印之logging.config.dictConfig使用总结

    #实践环境 WIN 10 Python 3.6.5 #函数说明 logging.config.dictConfig(config) dictConfig函数位于logging.config模块,该函数 ...

  • C# log4net

    C# log4net

  • 日志库 winston 的学习笔记 - 创建一个使用 winston 的 Node.js 应用

    winston 被设计为一个简单且通用的日志库,支持多种传输. 传输本质上是日志的存储设备. 每个 winston 记录器都可以在不同级别配置多个存储渠道.例如,人们可能希望将错误日志存储在持久的远程 ...

  • PHP设计模式之责任链模式

    PHP设计模式之责任链模式 责任链模式,属于对象行为型的设计模式. Gof类图及解释 GoF定义:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这 ...

  • [PHP小课堂]PHP设计模式之责任链模式

    [PHP小课堂]PHP设计模式之责任链模式 关注公众号:[硬核项目经理]获取最新文章 添加微信/QQ好友:[DarkMatterZyCoder/149844827]免费得PHP.项目管理学习资料

  • 通俗易懂系列 | 设计模式(六):责任链模式

    责任链设计模式是行为设计模式之一. 责任链模式用于在软件设计中实现松散耦合,其中来自客户端的请求被传递到对象链以处理它们.然后链中的对象将自己决定谁将处理请求以及是否需要将请求发送到链中的下一个对象. ...

  • 【5/25】责任链模式(Chain of Responsibility Pattern)

    这是<小游戏从0到1设计模式重构>系列内容第5篇,所有源码及资料在"程序员LIYI"公号回复"小游戏从0到1"获取. 这一小节我们尝试应用责任链模式 ...

  • 三国演义:责任链模式

    单例模式 模板方法模式 装饰器模式 门面模式 策略模式 委派模式 故事 前两天,没事又刷了一遍三国演义,看到关羽身在曹营心在汉,听说刘备在袁绍那里,然后就上演了"过五关,斩六将". ...

  • 责任链模式

    一.责任链模式介绍 1.定义与类型 为请求创建一个接收此次请求对象的链 类型:行为型 2.适用场景 一个请求的处理需要多个对象当中的一个或几个协作处理 3.优点 请求的发送者和接收者(请求的处理)解耦 ...

  • 设计模式(13) 职责链模式

    行为型模式 行为型模式关注于应用运行过程中算法的提供和通信关系的梳理. 相比于创建型模式和结构型模式,行为型模式包含了最多的设计模式种类,包括: 职责链模式 模板方法模式 解释器模式 命令模式 迭代器 ...

  • 责任链设计模式案例详解,看这一篇就够了

    老哥哔哔哔 责任链设计模式在日常工作中很常见,也很重要,很多开源框架都采用了责任链设计模式,比如:servlet中的Filter.Dubbo中的Filter.Mybatis中的Plugin.sprin ...

  • 设计模式-责任链设计模式

    责任链设计模式,是行为型设计模式的巅峰之作. 现在有一个场景,请假申请.请假时间的长短,需要不同级别的领导才能审批. 万物皆对象嘛,请假需要工号.姓名.原因.时长.结果等等,那我们来定义一个请假的类. ...