Java Chain of Responsibility Pattern(责任链模式)

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

关键代码:Handler 里面聚合它自己,在 HanleRequest 里判断是否合适,如果没达到条件则向下传递,向谁传递之前 set 进去。

优点: 1、降低耦合度。它将请求的发送者和接收者解耦。 2、简化了对象。使得对象不需要知道链的结构。 3、增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。 4、增加新的请求处理类很方便。
缺点: 1、不能保证请求一定被接收。 2、系统性能将受到一定影响,而且在进行代码调试时不太方便,可能会造成循环调用。 3、可能不容易观察运行时的特征,有碍于除错。

  1. 创建抽象的记录器类。
/**
 * 1. 创建抽象的记录器类。
 * @author mazaiting
 */
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);
        }
    }
    /**写消息*/
    protected abstract void write(String message);
    
    
}
  1. 创建扩展了该记录器类的实体类。
/**
 * 2. 创建扩展了该记录器类的实体类。
 * @author mazaiting
 */
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);
    }

}

/**
 * 2. 创建扩展了该记录器类的实体类。
 * @author mazaiting
 */
public class ErrorLogger extends AbstractLogger{

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

}

/**
 * 2. 创建扩展了该记录器类的实体类。
 * @author mazaiting
 */
public class FileLogger extends AbstractLogger{

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

}
  1. 创建不同类型的记录器。赋予它们不同的错误级别,并在每个记录器中设置下一个记录器。每个记录器中的下一个记录器代表的是链的一部分。
/**
 * 3. 创建不同类型的记录器。赋予它们不同的错误级别,并在每个记录器中设置下一个记录器。
 * 每个记录器中的下一个记录器代表的是链的一部分
 * @author mazaiting
 */
public class Client {
    public static void main(String[] args) {
        AbstractLogger loggerChain = 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.");
    }
    
    /**
     * 获取责任链
     */
    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;     
    }
}
  1. 打印结果
Standard Console::Logger: This is an information.
Standard File::Logger: This is an debug level information.
Standard Console::Logger: This is an debug level information.
Standard Error::Logger: This is an error information.
Standard File::Logger: This is an error information.
Standard Console::Logger: This is an error information.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值