<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 責任鏈模式 *責任鏈模式* 也許可以被看作一個使用了 *策略* 對象的“遞歸的動態一般化”。此時我們進行一次調用,在一個鏈序列中的每個策略都試圖滿足這個調用。這個過程直到有一個策略成功滿足該調用或者到達鏈序列的末尾才結束。在遞歸方法中,一個方法將反復調用它自身直至達到某個終止條件;使用責任鏈,一個方法會調用相同的基類方法(擁有不同的實現),這個基類方法將會調用基類方法的其他實現,如此反復直至達到某個終止條件。 除了調用某個方法來滿足某個請求以外,鏈中的多個方法都有機會滿足這個請求,因此它有點專家系統的意味。由于責任鏈實際上就是一個鏈表,它能夠動態創建,因此它可以看作是一個更一般的動態構建的 `switch` 語句。 在上面的 `StrategyPattern.java` 例子中,我們可能想自動發現一個解決方法。而 *責任鏈* 就可以達到這個目的: ```java // patterns/chain/ChainOfResponsibility.java // Using the Functional interface // {java patterns.chain.ChainOfResponsibility} package patterns.chain; import java.util.*; import java.util.function.*; class Result { boolean success; List<Double> line; Result(List<Double> data) { success = true; line = data; } Result() { success = false; line = Collections.<Double>emptyList(); } } class Fail extends Result {} interface Algorithm { Result algorithm(List<Double> line); } class FindMinima { public static Result leastSquares(List<Double> line) { System.out.println("LeastSquares.algorithm"); boolean weSucceed = false; if(weSucceed) // Actual test/calculation here return new Result(Arrays.asList(1.1, 2.2)); else // Try the next one in the chain: return new Fail(); } public static Result perturbation(List<Double> line) { System.out.println("Perturbation.algorithm"); boolean weSucceed = false; if(weSucceed) // Actual test/calculation here return new Result(Arrays.asList(3.3, 4.4)); else return new Fail(); } public static Result bisection(List<Double> line) { System.out.println("Bisection.algorithm"); boolean weSucceed = true; if(weSucceed) // Actual test/calculation here return new Result(Arrays.asList(5.5, 6.6)); else return new Fail(); } static List<Function<List<Double>, Result>> algorithms = Arrays.asList( FindMinima::leastSquares, FindMinima::perturbation, FindMinima::bisection ); public static Result minima(List<Double> line) { for(Function<List<Double>, Result> alg : algorithms) { Result result = alg.apply(line); if(result.success) return result; } return new Fail(); } } public class ChainOfResponsibility { public static void main(String[] args) { FindMinima solver = new FindMinima(); List<Double> line = Arrays.asList( 1.0, 2.0, 1.0, 2.0, -1.0, 3.0, 4.0, 5.0, 4.0); Result result = solver.minima(line); if(result.success) System.out.println(result.line); else System.out.println("No algorithm found"); } } /* Output: LeastSquares.algorithm Perturbation.algorithm Bisection.algorithm [5.5, 6.6] */ ``` 我們從定義一個 `Result` 類開始,這個類包含一個 `success` 標志,因此接收者就可以知道算法是否成功執行,而 `line` 變量保存了真實的數據。當算法執行失敗時, `Fail` 類可以作為返回值。要注意的是,當算法執行失敗時,返回一個 `Result` 對象要比拋出一個異常更加合適,因為我們有時可能并不打算解決這個問題,而是希望程序繼續執行下去。 每一個 `Algorithm` 接口的實現,都實現了不同的 `algorithm()` 方法。在 `FindMinama` 中,將會創建一個算法的列表(這就是所謂的“鏈”),而 `minima()` 方法只是遍歷這個列表,然后找到能夠成功執行的算法而已。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看