<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 問題 Problem 你很多方法用來解決某個問題,但是需要在運行時選擇或者切換這些方法。 You have more than one way to solve a problem but you need to choose (or even switch) between these methods at run time. ## 方法 Solution 將你的算法分裝到策略對象中。 Encapsulate your algorithms inside of Strategy objects. 例如,給定一個無序列表,我們可以在不同的情景中選擇不同的算法。 Given an unsorted list, for example, we can change the sorting algorithm under different circumstances. ### 基類 The base class: ~~~ class StringSorter constructor:(@algorithm)-> sort: (list) -> @algorithm list ~~~ ### 策略 The strategies: ~~~ bubbleSort = (list) -> anySwaps = false swapPass = -> for r in [0..list.length-2] if list[r] > list[r+1] anySwaps = true [list[r], list[r+1]] = [list[r+1], list[r]] swapPass() while anySwaps anySwaps = false swapPass() list reverseBubbleSort = (list) -> anySwaps = false swapPass = -> for r in [list.length-1..1] if list[r] < list[r-1] anySwaps = true [list[r], list[r-1]] = [list[r-1], list[r]] swapPass() while anySwaps anySwaps = false swapPass() list ~~~ ### 使用策略 Using the strategies: ~~~ sorter = new StringSorter bubbleSort unsortedList = ['e', 'b', 'd', 'c', 'x', 'a'] sorter.sort unsortedList # => ['a', 'b', 'c', 'd', 'e', 'x'] unsortedList.push 'w' # => ['a', 'b', 'c', 'd', 'e', 'x', 'w'] sorter.algorithm = reverseBubbleSort sorter.sort unsortedList # => ['a', 'b', 'c', 'd', 'e', 'w', 'x'] ~~~ ## 討論 Discussion “遇到敵人后一切戰斗計劃都失效了”,用戶也同樣,但是我們可以使用已掌握的知識來適應環境。例如,在上面的例子中,新加項打亂了興趣。了解細節,針對確切的場景,我們可以通過切換算法來加快排序,僅僅只需一次簡單的再賦值即可。 “No plan survives first contact with the enemy”, nor users, but we can use the knowledge gained from changing circumstances to adapt. Near the end of the example, for instance, the newest item in the array now lies out of order. Knowing that detail, we can then speed the sort up by switching to an algorithm optimized for that exact scenario with nothing but a simple reassignment. ### 練習 Exercises * 把`StringSorter`擴展成一個`AlwaysSortedArray`類,實現原生數組的所有功能,而且有通過插入方法新項時能夠自動地排序(例如,`push`和`shift`)。 * Expand?`StringSorter`?into an?`AlwaysSortedArray`?class that implements all of the functionality of a regular array but which automatically sorts new items based on the method of insertion (e.g.?`push`?vs.?`shift`).
                  <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>

                              哎呀哎呀视频在线观看