安卓笔记侠

专注安卓开发

导航

责任链模式

一.什么是责任链模式?

    责任链模式为请求创建一个接受者对象的链,这种模式给予请求的类型,对请求的发送者和接收者进行解耦.(将接受者对象连成一条链,并且在该链上传递请求,直到有一个接受者对象处理它,通过让更多对象有机会处理请求,避免了发送者和接受者之间的耦合度)这种类型的设计模式属于行为型模式。在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推.平常经常听说的击鼓传花,js中的事件冒泡,JAVA WEB 中 Apache Tomcat 对 Encoding 的处理,Struts2 的拦截器,jsp servlet 的 Filter等。

二.责任链模式实例实现?

   我们创建抽象类 AbstractLogger,带有详细的日志记录级别。然后我们创建三种类型的记录器,都扩展了 AbstractLogger。每个记录器消息的级别是否属于自己的级别,如果是则相应地打印出来,否则将不打印并把消息传给下一个记录器。这里我对测试类进行了调整,把初始化的方法提取到了LoggerFactory中,这样也可以体现设计中的单一职责原则。

   

   a.创建抽象的记录器类(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);
}

b.扩展记录器的实体类(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:: "+message);
    }
 
}

public class ErrorLogger extends AbstractLogger {
 
    public ErrorLogger(int level){
        this.level=level;
    }
    @Override
    protected void write(String message) {
        System.out.println("Error Console:: "+message);
         
    }
 
}

public class FileLogger extends AbstractLogger{
     
    public FileLogger(int level){
        this.level=level;
    }
 
    @Override
    protected void write(String message) {
        System.out.println("File Logger:: "+message);  
    }
 
}

LoggerFactory.java

public class LoggerFactory {
     
    public 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 class ChainPatternDemo {
    public static void main(String[] args) {
          AbstractLogger loggerChain =LoggerFactory.getChainOfLoggers();
          loggerChain.logMessage(AbstractLogger.INFO,
             "This is an information.");
          loggerChain.logMessage(AbstractLogger.DEBUG,
             "This is an debug level information.");
          loggerChain.logMessage(AbstractLogger.ERROR,
             "This is an error information.");
       }
}

   三.总结如下

     避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。职责链上的处理者负责处理请求,客户只需要将请求发送到职责链上即可,无须关心请求的处理细节和请求的传递,所以职责链将请求的发送者和请求的处理者解耦了。使用场景: 1、有多个对象可以处理同一个请求,具体哪个对象处理该请求由运行时刻自动确定。 2、在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。 3、可动态指定一组对象处理请求。

posted on 2016-11-14 01:16  安卓笔记侠  阅读(561)  评论(0编辑  收藏  举报