美文网首页
责任链模式

责任链模式

作者: 莫库施勒 | 来源:发表于2019-08-02 19:25 被阅读0次
    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);
       
    }
    
    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);
       }
    }
    
    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);
       }
    }
    
    public class FileLogger extends AbstractLogger {
     
       public FileLogger(int level){
          this.level = level;
       }
     
       @Override
       protected void write(String message) {    
          System.out.println("File::Logger: " + message);
       }
    }
    

    相关文章

      网友评论

          本文标题:责任链模式

          本文链接:https://www.haomeiwen.com/subject/pverdctx.html