1.意圖
在不破壞封裝性的前提下,捕獲一個對象的內部狀態(tài),并在該對象之外保存這個狀態(tài),這樣以后就可以將該對象恢復到先前保存的狀態(tài)。
2.結構和代碼

組織者,把原發(fā)器的狀態(tài)State(全部或者部分狀態(tài),一般是變量的值),通過CreateMemento()方法保存起來,繼續(xù)運行后,等待合適的時機,在通過SetMemento()方法可以再次恢復到之前的狀態(tài)。在這個過程中,我們并沒有對這些狀態(tài)做任何的訪問和設置,實際上這些狀態(tài)都是私有的,對外是禁止訪問的,我們只是通過Memento對象的兩個最簡單的方法就達到了這個效果。Memento經常寫成Originator的內部類。
在Android中,Canvas有兩個方法 save()和restore()方法再做圖形變換的時候使用的非常多,因為涉及到跨語言的問題,我不好就認定這個用的是備忘錄模式,但是它的這種思想絕對是備忘錄的思想。

我們來讀一讀它源代碼的注釋吧,首先看save()保存狀態(tài):
public class Canvas {
/**
* Saves the current matrix and clip onto a private stack. Subsequent
* calls to translate,scale,rotate,skew,concat or clip Rect,clipPath
* will all operate as usual, but when the balancing call to restore()
* is made, those calls will be forgotten, and the settings that existed
* before the save() will be reinstated.
*/
/**
*保存當前的矩陣和剪裁到一個私有的堆棧,其實矩陣和剪裁就是當前Canvas的狀態(tài)State
*/
public native int save();
}
再看恢復狀態(tài)restore():
public class Canvas {
/**
* This call balances a previous call to save(), and is used to remove all
* modifications to the matrix/clip state since the last save call. It is
* an error to call restore() more times than save() was called.
*/
/**
* 移除自上次保存操作后所做的修改,恢復到之前的狀態(tài),因為是堆棧實現(xiàn),所以pull操作不能不等于push操作,save()和restore()應該成對使用,否則恢復的狀態(tài)就很有可能是錯誤的
*/
public native void restore();
}
從上面的兩個方法中,它們實現(xiàn)了自我狀態(tài)的恢復,實際上我們只是執(zhí)行了兩個沒有接觸任何內部信息的方法,實際上這兩個方法就是在操作我們看不到的這些內部狀態(tài)信息。
3.效果
(1).保持封裝邊界,把很復雜的原發(fā)器的內部信息對外部其他對象隱藏起來。
(2).簡化的原發(fā)器,把狀態(tài)操作無形中轉化到客戶手里,簡化了原發(fā)器的某些實現(xiàn)。
(3).也要注意注意備忘錄的管理代價。