zoukankan      html  css  js  c++  java
  • 责任链模式

    模式介绍

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

    模式优点

    1、避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求。
    2、简化了对象,使得对象不需要知道链的结构。
    3、增强给对象指派职责的灵活性,通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。

    模式缺点

    1、不能保证请求一定被接收。
    2、系统性能将受到一定影响,而且在进行代码调试时不太方便,可能会造成循环调用。
    3、可能不容易观察运行时的特征,有碍于除错。

    使用场景

    1、有多个对象可以处理同一个请求,具体哪个对象处理该请求由运行时刻自动确定。
    2、在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。

    系统建模

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

    系统实现

    /**
     * AbstractLogger抽象类
     */
    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);   
    }
    
    /**
     * ConsoleLogger类
     */
    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);
       }
    }
    
    /**
     * ErrorLogger类
     */
    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);
       }
    }
    
    /**
     * FileLogger类
     */
    public class FileLogger extends AbstractLogger {
       public FileLogger(int level){
          this.level = level;
       }
     
       @Override
       protected void write(String message) {    
          System.out.println("File::Logger: " + message);
       }
    }
    
    /**
     * 客户端
     */
    public class Client {
       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.");
       }
    }
    结果:
    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.
    
  • 相关阅读:
    [django]Manipulator解惑
    [AJAX]Ajax.Net取Application的值问题
    IBatis.Net中为什么Output的paramMap的class设置为int就获取不到值
    [转]Python下载百度新歌100的代码
    [django]Django输出页面方式的补充
    Asp.Net的控件如何与Server交互
    [django]学习Model API的实例
    周杰伦的第七张专辑依然范特西
    立冬了,换个Skin
    InterDev调试asp页面和自定义activex all
  • 原文地址:https://www.cnblogs.com/feiqiangsheng/p/12248740.html
Copyright © 2011-2022 走看看