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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 備忘錄模式 Memento,在不破壞對象封裝性的前提下在對象外保存對象的某個狀態,以便在需要的時候將該對象恢復到該狀態。備忘錄模式可以用在需要保存或者恢復數據的相關狀態場景下,或者提供一個回滾操作(數據庫事務)。 ## 組件 1. Memento:包含了要被恢復的對象的狀態。 2. Originator:將狀態存儲進Memento中。 3. Caretaker:負責從 Memento 中恢復對象的狀態。 :-: ![](https://img.kancloud.cn/6b/83/6b83ec1a09bceeb34010fde4e864987a_560x360.png) ## 實現 1. 創建Memento類 ~~~ ?package net.smrobot.menento; ?? ?/** ? * 備忘錄模式 ? * 備忘錄類:包含要恢復的對象的狀態 ? */ ?? ?public class Memento { ? ? ?private String state; ? ? ? ? ? ?public Memento(String state) { ? ? ? ? ?this.state = state; ? ? } ? ? ? ? ? ?public String getState() { ? ? ? ? ?return state; ? ? } ?} ?? ~~~ 2. 創建Originator類 ~~~ ?package net.smrobot.menento; ?? ?/** ? * 源對象 ? * 在Memento對象中存儲狀態 ? */ ?public class Originator { ?? ? ? ?private String state; ?? ? ? ?public String getState() { ? ? ? ? ?return this.state; ? ? } ?? ? ? ?public void setState(String state) { ? ? ? ? ?this.state = state; ? ? } ?? ? ? ?/** ? ? ? * 將狀態存儲到Memento中 ? ? ? * @param state ? ? ? * @return ? ? ? */ ? ? ?public Memento saveStateToMemento(String state) { ? ? ? ? ?return new Memento(state); ? ? } ?? ? ? ?/** ? ? ? * 從Memento中獲取狀態 ? ? ? * @param memento ? ? ? */ ? ? ?public void getStateFromMemento(Memento memento) { ? ? ? ? ?this.state = memento.getState(); ? ? } ?? ?} ?? ~~~ 3. 創建Caretaker類 ~~~ ?package net.smrobot.menento; ?? ?import java.util.ArrayList; ?import java.util.List; ?? ?/** ? * 負責從Memento中恢復對象的狀態 ? */ ?public class Caretaker { ?? ? ? ?private List<Memento> mementoList = new ArrayList<>(); ?? ? ? ?public void add(Memento memento) { ? ? ? ? ?mementoList.add(memento); ? ? } ?? ? ? ?public Memento get(int index) { ? ? ? ? ?if (index < 0 || index >= mementoList.size()) { ? ? ? ? ? ? ?return null; ? ? ? ? } ? ? ? ? ?return mementoList.get(index); ? ? } ?? ?} ?? ~~~ 備忘錄模式其實就是在要保存狀態的對象中添加兩個方法用戶將狀態保留進備忘錄中或者從備忘錄中獲取狀態。然后將所有的備忘錄添加到一個容器進行維護。
                  <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>

                              哎呀哎呀视频在线观看