iOS Design mode-Memo
Schematic diagram
Description
1. Without destroying the package, capture the internal state of an object and save the state outside of that object so that it can be restored to its previously saved state
2. I have abstracted the logic of creating state and recovery state into protocols and working with the memo center
Source
Https://github.com/YouXianMing/MementoPattern
////MementoCenter.h//Mementopattern////Created by youxianming on 15/9/11.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>#import "MementoCenterProtocol.h"@interfaceMementocenter:nsobject/** * Save Memo Object * * @param mementoobject Memo Object * @param key value of key tag object*/+ (void) Savemementoobject: (ID<MementoCenterProtocol>) Mementoobject Withkey: (NSString *) key;/** * Get Memo Object * * @param key value of Tag object * * @return Memo Object*/+ (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>@implementationMementocenter+ (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 { returnNil; }}#pragmamark-md5+ (NSString *) md5hashwithstring: (NSString *)string{cc_md5_ctx MD5; Cc_md5_init (&MD5); Cc_md5_update (&MD5, [stringUtf8string], (Cc_long) [stringlength]); unsignedCharDigest[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[Ten], digest[ One], digest[ A], digest[ -], digest[ -], digest[ the]]; returns;}#pragmaMark-Local Persistence + (void) Storevalue: (ID) value with: (NSString *) key {[[Nsuserdefaults standarduserdefaults] Setobject:value forkey:key];}+ (ID) Valuewithkey: (NSString *) Key {return[[Nsuserdefaults standarduserdefaults] objectforkey:key];}#pragmaMark-use Fastcode to process objects + (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>@protocolMementocenterprotocol <NSObject>@required/** * Generate current Status object value * * @return Object value*/- (ID) CurrentState;/** * Restore to the specified status * * @param state*/- (void) Recoverfromstate: (ID) state;@end
Analysis
Use Fastcoder as a storage tool
iOS Design mode-Memo