iOS设计模式 - 备忘录
原理图
说明
1. 在不破坏封装的情况下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存的状态
2. 本人已经将创建状态与恢复状态的逻辑抽象成了协议,并配合备忘录中心一起使用
源码
https://github.com/YouXianMing/iOS-Design-Patterns
// // MementoCenter.h // MementoPattern // // Created by YouXianMing on 15/9/11. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> #import "MementoCenterProtocol.h" @interface MementoCenter : NSObject /** * 存储备忘录对象 * * @param mementoObject 备忘录对象 * @param key 标记对象的键值 */ + (void)saveMementoObject:(id <MementoCenterProtocol>)mementoObject withKey:(NSString *)key; /** * 获取备忘录对象 * * @param key 标记对象的键值 * * @return 备忘录对象 */ + (id)mementoObjectWithKey:(NSString *)key; @end
// // MementoCenter.m // MementoPattern // // Created by YouXianMing on 15/9/11. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "MementoCenter.h" #import "FastCoder.h" #import <CommonCrypto/CommonDigest.h> @implementation MementoCenter + (void)saveMementoObject:(id <MementoCenterProtocol>)mementoObject withKey:(NSString *)key { NSParameterAssert(mementoObject); NSParameterAssert(key); NSData *data = [self makeDataWithValue:[mementoObject currentState]]; if (data) { [self storeValue:data with:[self MD5HashWithString:key]]; } } + (id)mementoObjectWithKey:(NSString *)key { NSParameterAssert(key); NSData *data = [self valueWithKey:[self MD5HashWithString:key]]; if (data) { return [self makeObjectWithData:data]; } else { return nil; } } #pragma mark - md5 + (NSString *)MD5HashWithString:(NSString *)string { CC_MD5_CTX md5; CC_MD5_Init(&md5); CC_MD5_Update(&md5, [string UTF8String], (CC_LONG) [string length]); unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5_Final(digest, &md5); NSString *s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7], digest[8], digest[9], digest[10], digest[11], digest[12], digest[13], digest[14], digest[15]]; return s; } #pragma mark - 本地持久化 + (void)storeValue:(id)value with:(NSString *)key { [[NSUserDefaults standardUserDefaults] setObject:value forKey:key]; } + (id)valueWithKey:(NSString *)key { return [[NSUserDefaults standardUserDefaults] objectForKey:key]; } #pragma mark - 使用FastCode来处理对象 + (NSData *)makeDataWithValue:(id)value { return [FastCoder dataWithRootObject:value]; } + (id)makeObjectWithData:(NSData *)data { return [FastCoder objectWithData:data]; } @end
// // MementoCenterProtocol.h // MementoPattern // // Created by YouXianMing on 15/9/11. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @protocol MementoCenterProtocol <NSObject> @required /** * 生成当前状态对象值 * * @return 对象值 */ - (id)currentState; /** * 恢复到指定的状态 * * @param state 状态 */ - (void)recoverFromState:(id)state; @end
分析
使用FastCoder来作为存储工具