18.ChainOfResponsibility-職責鏈模式

Chain Of Responsibility 職責鏈模式

  • 職責鏈模式:
    使多個對象都有機會處理請求,從而避免請求的發送者和接受者之間的耦合關係,
    將這個對象連成一條鏈,並沿着個鏈傳遞請求,直到有一個對象處理爲止。
    發出這個請求的客戶端並不知道鏈上的哪個對象最終處理這個請求,這使得系統能夠在不影響客戶端的狀況下動態地從新組織鏈和分配責任。java

  • 使用場景:markdown

    1. 有多個的對象能夠處理一個請求,哪一個對象處理該請求運行時刻自動肯定。
    2. 你想在不明確指定接收者的狀況下,向多個對象中的一個提交一個請求。
    3. 可動態指定一組對象處理請求。
  • 示例類圖:
    ChainOfResponsibility_umlide

  • 示例代碼:性能

// Handler
public abstract class AbstractLogger {
    public static final int INFO = 1;
    public static final int DEBUG = 2;
    public static final 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) {
        if (this.level <= level) {
            write(message);
        }
        if (nextLogger != null) {
            nextLogger.logMessage(level, message);
        }
    }

    protected abstract 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);
    }
}

// 測試
public class ChainTest {

    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 static void main(String[] args) {
        AbstractLogger loggerChain = getChainOfLoggers();
        loggerChain.logMessage(AbstractLogger.INFO, "information.");
        loggerChain.logMessage(AbstractLogger.DEBUG, "debug level information.");
        loggerChain.logMessage(AbstractLogger.ERROR, "error information.");
    }
}
  • 抽象處理者(Handler)角色:
    定義出一個處理請求的接口。若是須要,接口能夠定義 出一個方法以設定和返回對下家的引用。
    這個角色一般由一個Java抽象類或者Java接口實現。上圖中Handler類的聚合關係給出了具體子類對下家的引用,
    抽象方法 handleRequest()規範了子類處理請求的操做。測試

  • 具體處理者(ConcreteHandler)角色:
    具體處理者接到請求後,能夠選擇將請求處理掉,或者將請求傳給下家。因爲具體處理者持有對下家的引用,
    所以,若是須要,具體處理者能夠訪問下家。this

  • 優勢:
    在於能夠下降系統的耦合度,簡化對象的相互鏈接,同時加強給對象指派職責的靈活性,增長新的請求處理類也很方便。spa

  • 缺點:
    不能保證請求必定被接收,且對於比較長的職責鏈,請求的處理可能涉及到多個處理對象,系統性能將受到必定影響,並且在進行代碼調試時不太方便。debug

相關文章
相關標籤/搜索