欢迎来到Vincentyw的博客

该来的终究会来,该走的也一定会离开。凡事都存在前因后果,因缘成熟了,果报便产生了,无法阻挡。
但是发生过了就会消失,有来就有走,一切都是过客,把握自己当下的因缘,承担起自己该承担的责任,做好眼前该做的事情,做的时候尽全力,过去便放下,放下即自在。

设计模式之责任链模式

Chain of Responsibility(责任链)

一、责任链模式简介

责任链模式(Chain of Responsibility)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。

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

核心:

  • 1、避免请求发送者与接收者耦合在一起
  • 2、拦截的类都实现统一接口
  • 3、Handler 里面聚合它自己,在 HandlerRequest 里判断是否合适,如果没达到条件则向下传递,向谁传递之前 set 进去

二、责任链模式优缺点比较

优点:

  • 1、降低耦合度。它将请求的发送者和接收者解耦
  • 2、简化了对象。使得对象不需要知道链的结构
  • 3、增强给对象指派职责的灵活性
  • 4、增加新的请求处理类很方便

缺点:

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

使用场景:

  • 1、单据报销、供应链管理
  • 2、拦截器、过滤器
  • 3、try catch捕获机制

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

三、UML类图实现

 

四、设计实现

     步骤1:定义抽象类

public abstract class AbstractLogger {
    
    protected static int INFO = 1;
    protected static int DEBUG = 2;
    protected static int ERROR = 3;
    
    protected int level ;
    
    protected AbstractLogger nextLogger;

    public void setNextLogger(AbstractLogger nextLogger) {
        this.nextLogger = nextLogger;
    }
    
    public void logMsg(int level,String msg) {
        System.out.println("---thisLevel="+this.level+"---level="+level);
        if(this.level == level) {
            write(msg);
        }else {
            if(nextLogger!=null) {
                nextLogger.logMsg(level, msg);
            }
        }
    }
    
    abstract  void write(String msg);
}

 步骤2:定义继承抽象类的具体类

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

    @Override
    void write(String msg) {
        System.out.println("console logger::write  "+msg);
    }

}


public class DebugLogger extends AbstractLogger {
    
    public DebugLogger(int level) {
        this.level = level;
    }

    @Override
    void write(String msg) {
        System.out.println("debug logger::write  "+msg);
    }
}

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

    @Override
    void write(String msg) {
        System.out.println("error logger::write  "+msg);
    }
}

 步骤3:调用者测试

public class ChainPatternDemo {
    public static AbstractLogger getChainLogger() {
        AbstractLogger c = new ConsoleLogger(AbstractLogger.INFO);
        AbstractLogger d = new DebugLogger(AbstractLogger.DEBUG);
        AbstractLogger e = new ErrorLogger(AbstractLogger.ERROR);
        e.setNextLogger(d);
        d.setNextLogger(c);
        return e;
    }
    
    public static void main(String[] args) {
        AbstractLogger logger = getChainLogger();
        logger.logMsg(AbstractLogger.INFO, "This is an information.");
         
        logger.logMsg(AbstractLogger.DEBUG,  "This is a debug level information.");
     
        logger.logMsg(AbstractLogger.ERROR,  "This is an error information.");
       }
}

 步骤4:运行程序,观察结果

 

 

 

 

posted on 2020-04-07 20:01  VincentYew  阅读(205)  评论(0)    收藏  举报

导航