- 備忘錄模式
設(shè)計(jì)存儲(chǔ)中心来惧,指定存儲(chǔ)接口演顾,實(shí)現(xiàn)存儲(chǔ)機(jī)制隅居。- 優(yōu)化存儲(chǔ)方案
統(tǒng)一存儲(chǔ)規(guī)范,實(shí)現(xiàn)靈活多變的存儲(chǔ)機(jī)制胎源。
- 優(yōu)化存儲(chǔ)方案
FastCoder 本地序列化,轉(zhuǎn)NSData宪卿,比對(duì)象實(shí)現(xiàn)NSCopying存儲(chǔ)好
- 應(yīng)用万栅,使用場(chǎng)景
存儲(chǔ)UIView的狀態(tài),
數(shù)據(jù)庫(kù)回退
備忘錄中心
//
// MementoCenter.h
// LearnMemento
//
// Created by 印林泉 on 2017/3/8.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MementoCenterProtocol.h"
@interface MementoCenter : NSObject
/**
存儲(chǔ)備忘錄對(duì)象
@param object 值
@param key 鍵
*/
+ (void)saveMementoObject:(id<MementoCenterProtocol>)object withKey:(NSString *)key;
/**
獲取備忘錄對(duì)象
@param key 鍵
@return 值
*/
+ (id)mementoObjectWithKey:(NSString *)key;
@end
//
// MementoCenter.m
// LearnMemento
//
// Created by 印林泉 on 2017/3/8.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "MementoCenter.h"
@implementation MementoCenter
+ (void)saveMementoObject:(id)object withKey:(NSString *)key {
}
+ (id)mementoObjectWithKey:(NSString *)key {
return nil;
}
@end
備忘錄中心協(xié)議休溶,存儲(chǔ)的對(duì)象必須滿(mǎn)足這個(gè)協(xié)議扰她,才能存儲(chǔ)到備忘錄中心
//
// MementoCenterProtocol.h
// LearnMemento
//
// Created by 印林泉 on 2017/3/8.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol MementoCenterProtocol <NSObject>
/**
獲取狀態(tài)
@return 狀態(tài)值
*/
- (id)currentState;
/**
從某種狀態(tài)恢復(fù)
@param state 狀態(tài)值
*/
- (void)recoverFromState:(id)state;
@end
存儲(chǔ)對(duì)象(蘋(píng)果)
//
// Apple.h
// LearnMemento
//
// Created by 印林泉 on 2017/3/8.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MementoCenterProtocol.h"
@interface Apple : NSObject<MementoCenterProtocol>
@end
//
// Apple.m
// LearnMemento
//
// Created by 印林泉 on 2017/3/8.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "Apple.h"
@implementation Apple
- (id)currentState {
return self;
}
- (void)recoverFromState:(id)state {
}
@end
使用
//
// ViewController.m
// LearnMemento
//
// Created by 印林泉 on 2017/3/8.
// Copyright ? 2017年 ylq. All rights reserved.
//
#import "ViewController.h"
#import "Apple.h"
#import "MementoCenter.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Apple *apple = [[Apple alloc] init];
///save
[MementoCenter saveMementoObject:[apple currentState] withKey:@"Apple"];
///get
[apple recoverFromState:[MementoCenter mementoObjectWithKey:@"Apple"]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end