iOS Design mode-Memo

Source: Internet
Author: User

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 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.