設(shè)計模式備忘錄模式學(xué)習(xí)教案_第1頁
設(shè)計模式備忘錄模式學(xué)習(xí)教案_第2頁
設(shè)計模式備忘錄模式學(xué)習(xí)教案_第3頁
設(shè)計模式備忘錄模式學(xué)習(xí)教案_第4頁
設(shè)計模式備忘錄模式學(xué)習(xí)教案_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、會計學(xué)1設(shè)計模式設(shè)計模式(msh)備忘錄模式備忘錄模式(msh)第一頁,共26頁。取出事先保存的歷史狀態(tài)來覆蓋當前狀態(tài)第2頁/共26頁第二頁,共26頁。備忘錄模式:備忘錄模式:在不破壞封裝的前提下在不破壞封裝的前提下,捕獲一個對象的內(nèi)部狀態(tài),并在該對象之外保存這個狀態(tài),這樣就可以在以后可以在以后將對象恢復(fù)到原先保存的狀態(tài)將對象恢復(fù)到原先保存的狀態(tài)。Memento Pattern: Without violating encapsulation, capture and externalize an objects internal state so that the object can be

2、 restored to this state later.第3頁/共26頁第三頁,共26頁。第4頁/共26頁第四頁,共26頁。第5頁/共26頁第五頁,共26頁。第6頁/共26頁第六頁,共26頁。namespace MementoSample public class Originator private string state; public Originator(string state) this.state = state; / 創(chuàng)建一個備忘錄對象創(chuàng)建一個備忘錄對象 internal Memento CreateMemento() return new Memento(this);

3、/ 根據(jù)備忘錄對象恢復(fù)原發(fā)器狀態(tài)根據(jù)備忘錄對象恢復(fù)原發(fā)器狀態(tài) internal void RestoreMemento(Memento m) state = m.GetState(); public void SetState(string state) this.state=state; public string GetState() return this.state; 第7頁/共26頁第七頁,共26頁。namespace MementoSample /備忘錄類,默認可見性備忘錄類,默認可見性,在在程序程序集內(nèi)可見集內(nèi)可見 internal class Memento private s

4、tring state; internal Memento(Originator o) state = o.GetState(); internal void SetState(string state) this.state = state; internal string GetState() return this.state; 第8頁/共26頁第八頁,共26頁。n理想的情況是只允許(ynx)生成該備忘錄的原發(fā)器訪問備忘錄的內(nèi)部狀態(tài)第9頁/共26頁第九頁,共26頁。訪問備忘錄中的數(shù)據(jù),其他對象都無法使用備忘錄中的數(shù)據(jù)第10頁/共26頁第十頁,共26頁。namespace MementoS

5、ample public class Caretaker private Memento memento; internal Memento GetMemento() return memento; internal void SetMemento(Memento memento) this.memento = memento; 第11頁/共26頁第十一頁,共26頁。using System;namespace MementoSample class Program static void Main(string args) /創(chuàng)建原發(fā)器對象創(chuàng)建原發(fā)器對象 Originator ori = n

6、ew Originator(狀態(tài)狀態(tài)(1); Console.WriteLine(ori.GetState(); /創(chuàng)建負責(zé)人對象,保存創(chuàng)建的備忘錄對象創(chuàng)建負責(zé)人對象,保存創(chuàng)建的備忘錄對象 Caretaker ct = new Caretaker(); ct.SetMemento(ori.CreateMemento(); ori.SetState(狀態(tài)狀態(tài)(2); Console.WriteLine(ori.GetState(); /從負責(zé)人對象中取出備忘錄對象,實現(xiàn)撤銷從負責(zé)人對象中取出備忘錄對象,實現(xiàn)撤銷 ori.RestoreMemento(ct.GetMemento(); Consol

7、e.WriteLine(ori.GetState(); Console.Read(); 狀態(tài)(1)狀態(tài)(2)狀態(tài)(1)第12頁/共26頁第十二頁,共26頁。某軟件公司要使用C#語言開發(fā)一款可以運行在Windows Phone移動平臺的觸摸式中國象棋軟件,由于考慮到有些用戶是“菜鳥”,經(jīng)常不小心走錯棋;還有些用戶因為不習(xí)慣使用手指在手機屏幕上拖動棋子,常常出現(xiàn)操作失誤,因此該中國象棋軟件要提供“悔棋”功能,在用戶走錯棋或操作失誤后可恢復(fù)到前一個步驟。如下圖所示: 中國象棋軟件界面示意圖中國象棋軟件界面示意圖為了實現(xiàn)“悔棋”功能,現(xiàn)使用備忘錄模式來設(shè)計該中國象棋軟件。第13頁/共26頁第十三頁,共

8、26頁。中國象棋棋子中國象棋棋子(qz)撤銷功能結(jié)構(gòu)圖撤銷功能結(jié)構(gòu)圖第14頁/共26頁第十四頁,共26頁。演示演示參考參考(cnko)代碼代碼 (DesignPatternMementoSample)第15頁/共26頁第十五頁,共26頁。第16頁/共26頁第十六頁,共26頁。重做(Redo)或恢復(fù)操作,即取消撤銷,讓對象狀態(tài)得到恢復(fù)第17頁/共26頁第十七頁,共26頁。改進之后的中國象棋棋子撤銷改進之后的中國象棋棋子撤銷(chxio)功能結(jié)構(gòu)圖功能結(jié)構(gòu)圖第18頁/共26頁第十八頁,共26頁。using System.Collections;namespace MementoSample pub

9、lic class MementoCaretaker /定義一個集合來存儲多個備忘錄定義一個集合來存儲多個備忘錄 private ArrayList mementolist = new ArrayList(); internal ChessmanMemento GetMemento(int i) return (ChessmanMemento)mementolisti; internal void SetMemento(ChessmanMemento memento) mementolist.Add(memento); 第19頁/共26頁第十九頁,共26頁。演示演示參考參考(cnko)代碼代碼 (DesignPatternMementoExtend)第20頁/共26頁第二十頁,共26頁。(di

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論