廣告

2015年4月7日 星期二

[design pattern] [java] Chain of Responsibility Pattern

責任鏈模式物件導向程式設計裡是一種軟體設計模式
它包含了一些命令對象和一系列的處理對象。
每一個處理對象決定它能處理哪些命令對象,
它也知道如何將它不能處理的命令對象傳遞給該鏈中的下一個處理對象。
該模式還描述了往該處理鏈的末尾添加新的處理對象的方法



AbstractLogger.java
package com.test.chainofresponsibilitypattern;

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

 protected int level;

 // next element in chain or responsibility
 protected AbstractLogger nextLogger;

 public void setNextLogger(AbstractLogger nextLogger) {
  this.nextLogger = nextLogger;
 }

 public void logMessage(int level, String message) {
  System.out.println("current class :"+this.getClass().getSimpleName()+"  "+
                     "current(this).level : " + this.level + "  " + 
               "level(from function) = "+ level);

  if (this.level <= level) {
   write(message);
  }
  if (nextLogger != null) {
   //find out whether there have "next"
   nextLogger.logMessage(level, message);
  }
 }

 abstract protected void write(String message);

}




FileLogger.java
package com.test.chainofresponsibilitypattern;

public class FileLogger extends AbstractLogger {

 public FileLogger(int level) {
  this.level = level;
 }

 @Override
 protected void write(String message) {
  System.out.println("File Logger: " + message);
 }
}



ConsoleLogger.java
package com.test.chainofresponsibilitypattern;

public class ConsoleLogger extends AbstractLogger {

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

 @Override
 protected void write(String message) {
  System.out.println("Console Logger: " + message);
 }
}



ErrorLogger.java
package com.test.chainofresponsibilitypattern;

public class ErrorLogger extends AbstractLogger {

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

 @Override
 protected void write(String message) {
  System.out.println("Error Logger: " + message);
 }
}


ChainPatterntest.java
package com.test.chainofresponsibilitypattern;

public class ChainPatterntest {
 
    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;
       //order is important
       //return the first object you set
    }

    public static void main(String[] args) {
       AbstractLogger loggerChain = getChainOfLoggers();

       loggerChain.logMessage(AbstractLogger.INFO, 
          "This is an information.");

       System.out.println("======================");
       loggerChain.logMessage(AbstractLogger.DEBUG, 
          "This is an debug information.");

       System.out.println("======================");
       loggerChain.logMessage(AbstractLogger.ERROR, 
          "This is an error information.");
    }
 }



console
current class :ErrorLogger  current(this).level : 3  level(from function) = 1
current class :FileLogger  current(this).level : 2  level(from function) = 1
current class :ConsoleLogger  current(this).level : 1  level(from function) = 1
Console Logger: This is an information.
======================
current class :ErrorLogger  current(this).level : 3  level(from function) = 2
current class :FileLogger  current(this).level : 2  level(from function) = 2
File Logger: This is an debug information.
current class :ConsoleLogger  current(this).level : 1  level(from function) = 2
Console Logger: This is an debug information.
======================
current class :ErrorLogger  current(this).level : 3  level(from function) = 3
Error Logger: This is an error information.
current class :FileLogger  current(this).level : 2  level(from function) = 3
File Logger: This is an error information.
current class :ConsoleLogger  current(this).level : 1  level(from function) = 3
Console Logger: This is an error information.



沒有留言:

張貼留言