備忘錄模式,顧名思義,初衷就是為了返回上一個狀態而設計的。從名字看起來一目了然。先給出定義。
Memento(備忘錄):在不破壞封裝性的前提下,捕獲一個物件的內部狀態,並在該物件之外保存這個狀態,這樣以後就可將該物件恢復到原先保存的狀態。
定義看起來很專業,其實就是保存上一個狀態以便日後恢復。好比在玩遊戲,打大 Boss 之前擔心第一次打不過,先存個檔,萬一失敗了還可以恢復狀態重新挑戰。
下面給出類別結構圖。

Originator(原發器):記錄當前時刻的內部狀態,負責定義哪些屬於需要備份的狀態,負責創建 memento,負責從 memento 恢復狀態。
Memento(備忘錄):負責存儲 Originator 的內部狀態,在需要時提供給 Originator。
Caretaker(看管人):將 Memento 保存在安全的地方,並負責提取。
一句話概括:Originator 創建一個包含其狀態的 Memento 交給 Caretaker 保管,Caretaker 不知如何與 Memento 交互,只負責把 Memento 在安全的地方保存好。
那麼備忘錄模式一般用在什麼場合呢?它比較適用於功能比較複雜、但需要維護或記錄屬性歷史的類,或者需要保存的屬性只是眾多屬性中的一小部分時,Originator 可以根據保存的 Memento 資訊還原到前一狀態。有時候一些物件的內部資訊必須保存在物件以外的地方,但必須由物件自己讀取,這時使用備忘錄可以把複雜的物件內部資訊對其他物件屏蔽起來。最大的作用還是在於:當角色的狀態改變時,有可能這個狀態無效,這時就可以使用暫時存儲起來的備忘錄將狀態復原。下面直接看實現代碼。
Objective-C 代碼實現:
Originator:
//發起人:記錄當前時刻的內部狀態,負責定義哪些屬於備份範圍的狀態,負責創建和恢復備忘錄數據。 #import <Foundation/Foundation.h> @class NimoMemento; @interface NimoOriginator : NSObject @property (nonatomic, copy) NSString* state; - (NimoMemento *)createMemento; - (void)restoreMemento:(NimoMemento *)memento; @end
#import "NimoOriginator.h"
#import "NimoMemento.h"
@implementation NimoOriginator
- (NimoMemento *)createMemento
{
NimoMemento *memento = [[NimoMemento alloc] initWithState:_state];
return memento;
}
- (void)restoreMemento:(NimoMemento *)memento
{
_state = memento.state;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"State:%@", _state];
}
@end
Memento:
//備忘錄:負責存儲發起人物件的內部狀態,在需要的時候提供發起人需要的內部狀態。 #import <Foundation/Foundation.h> @interface NimoMemento : NSObject @property (nonatomic, copy, readonly) NSString *state; - (id)initWithState:(NSString *)state; @end
#import "NimoMemento.h" @interface NimoMemento() @property (nonatomic, copy, readwrite) NSString *state; @end
@implementation NimoMemento
- (id)initWithState:(NSString *)state
{
if (self = [super init]) {
_state = [state copy];
}
return self;
}
@end
Caretaker:
//管理角色:對備忘錄進行管理,保存和提供備忘錄。 #import <Foundation/Foundation.h> @class NimoMemento; @interface NimoCaretaker : NSObject @property (nonatomic, assign) NimoMemento *memento; @end
// // NimoCaretaker.m // MementoDemo // #import "NimoCaretaker.h" @implementation NimoCaretaker @end
Client:
#import <Foundation/Foundation.h>
#import "NimoOriginator.h"
#import "NimoMemento.h"
#import "NimoCaretaker.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NimoOriginator *originator = [[NimoOriginator alloc] init];
originator.state = @"Old";
NSLog(@"%@", originator);
NimoMemento *memento = [originator createMemento];
NimoCaretaker *caretaker = [[NimoCaretaker alloc] init];
caretaker.memento = memento;
originator.state = @"New";
NSLog(@"%@", originator);
[originator restoreMemento:[caretaker memento]];
NSLog(@"%@", originator);
}
return 0;
}
運行結果:
2015-08-12 20:27:39.184 MementoDemo[1160:34914] State:Old 2015-08-12 20:27:39.186 MementoDemo[1160:34914] State:New 2015-08-12 20:27:39.186 MementoDemo[1160:34914] State:Old
以上通用代碼運行後雖然能得到期望的結果,但並不完美。在 Memento 類的實現中,我們把 state 屬性以及 initWithState 初始化方法暴露在了公共介面中,而這兩者本應只提供給 Originator 與 Memento(即對 Originator 與 Memento 提供寬介面,對 Caretaker 等其他物件提供窄介面)。在 C++ 等其他物件導向語言中,一般使用 private 或 friend 進行聲明,但在 Objective-C 中一切都是公有的,所以需要額外的技巧來實現。
可以透過類擴展(Class Extension)把 state 屬性以及 initWithState 初始化方法從主介面頭文件 NimoMemento.h 中分離出來:
// // NimoMemento+Private.h // MementoDemo // #import "NimoMemento.h" @interface NimoMemento () @property (nonatomic, copy, readwrite) NSString *state; - (id)initWithState:(NSString *)state; @end
如此,只在 Originator 與 Memento 中 #import NimoMemento+Private.h,就實現了介面的私有化,讓 Caretaker 只能透過窄介面存取備忘錄,兼顧了封裝性與安全性。