iOS design mode---memo mode

Source: Internet
Author: User

Memo mode: captures the internal state of an object without destroying the package, and saves the state outside the object. The object can then be restored to its previously saved state.

Originator: Records the internal state of the current moment, is responsible for defining which state is required for backup, is responsible for creating memento, and is responsible for recovering the state from Memento.

Memento (Memo): Responsible for storing the internal state of the originator, provided to the originator internal state when needed.

Caretaker (custodian): Keep memento in a safe place and be responsible for extraction.

In a nutshell: Originator create a memento that contains its status to caretaker custody, caretaker do not know how to interact with memento, only responsible for the memento in a safe place to save.

Objective-c Code implementation:

Originator:

////  nimooriginator.h//   mementodemo////  created by tony on 15/8/12.//  copyright  (c)  2015  nimoworks. all rights reserved.////Initiator: Records the internal state of the current moment and is responsible for defining which state is part of the backup scope. Responsible for creating and recovering Memo data. #import  <Foundation/Foundation.h> @class  NimoMemento; @interface  nimooriginator :  NSObject@property  (nonatomic, copy)  NSString* state;-  (nimomemento *) creatememento;-  (void) Restorememento: (nimomemento *) memento; @end 
nimooriginator.m//mementodemo////Created by Tony on 15/8/12.//Copyright (c) 2015 Nimoworks. All rights reserved.//#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:

////  nimomemento.h//   mementodemo////  created by tony on 15/8/12.//  copyright  (c) &NBSP;2015 Year  nimoworks. all rights reserved.//Memo: responsible for storing the internal state of the initiator object, providing the internal state required by the initiator when needed. #import  <Foundation/Foundation.h> @interface  NimoMemento : NSObject@property  ( NONATOMIC,&NBSP;COPY,&NBSP;READONLY)  NSString *state;-  (ID) initwithstate: (nsstring *) state ; @end 
nimomemento.m//mementodemo////Created by Tony on 15/8/12.//Copyright (c) 2015 Nimoworks. All rights reserved.//#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:

nimocaretaker.h//mementodemo////Created by Tony on 15/8/12.//Copyright (c) 2015 Nimoworks. All rights reserved.//Management roles: Manage memos, save and provide memos. #import <Foundation/Foundation.h> @class nimomemento; @interface Nimocaretaker:nsobject@property (Nonatomic, assign) Nimomemento *memento; @end
nimocaretaker.m//mementodemo////Created by Tony on 15/8/12.//Copyright (c) 2015 Nimoworks. All rights reserved.//#import "NimoCaretaker.h" @implementation nimocaretaker@end

Client:

&NBSP;&NBSP;MAIN.M//&NBSP;&NBSP;MEMENTODEMO////&NBSP;&NBSP;CREATED&NBSP;BY&NBSP;TONY&NBSP;ON&NBSP;15/8/12.   Copyright  (c)  2015 year  nimoworks. all rights reserved.//#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;}

Run:

2015-08-12 20:27:39.184 mementodemo[1160:34914] state:old2015-08-12 20:27:39.186 MementoDemo[1160:34914] State: new2015-08-12 20:27:39.186 mementodemo[1160:34914] State:old

Although the above-mentioned general code can get the desired result, but it is not perfect, in the implementation of the Menmento class, we expose the State property and the Initwithstate initialization method to the public interface, both of which should be provided only to originator and Menmento (That is, it provides a wide interface to originator and Menmento, and a narrow interface to other objects such as caretaker). In other object-oriented languages such as C + +, it is common to declare with private or friend. But in objective-c everything is public, so there is an extra skill to implement.

The State property and the Initwithstate initialization method are separated from the primary oral file NimoMemento.h through the class extension:

nimomemento+private.h//mementodemo////Created by Tony on 15/8/13.//Copyright (c) 2015 Nimoworks. All rights reserved.//#import "NimoMemento.h" @interface Nimomemento () @property (nonatomic, copy, ReadWrite) NSString * state;-(ID) initwithstate: (NSString *) state; @end

Thus, the privatization of the interface was realized only in the originator and Menmento #import nimomemento+private.h.

iOS design mode---memo mode

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.