Application of Memento memo mode in design mode in IOS app development _ios

Source: Internet
Author: User

Memo mode. As the name suggests, the memo pattern is designed to return to the previous state. From the name looks at a glance, OK, or the same as the old, first given the definition.

Memo (Memento): captures the internal state of an object without damaging the encapsulation, and saves the state outside the object. The object can then be restored to its previously saved state.

The definition seems to be very professional, in fact, is to save the last state for later recovery. Like playing a game, before playing Big Boss worried about the first fight before, save a disk, in case of play is over, can also restore the status of PK.

The class structure diagram is given below.

Originator (original): Records the internal state of the current moment, is responsible for defining what is required to back up the state, responsible for creating memento, responsible for the recovery from the memento state.

Memento (Memo): Responsible for storing the internal state of the originator and providing it to originator internal state when needed.

Caretaker (keeper): Keep Memento in a safe place and be responsible for extraction.

Sentence Summary: Originator create a memento containing its status to caretaker custody, caretaker do not know how to interact with the memento, only responsible for the memento in a safe place to keep good.
From the above picture, the relationship is relatively simple. So what are the typical scenarios for memo mode?

The memento pattern is useful for classes that have complex functions, but need to maintain or record the history of a property, or a property that needs to be saved is only a small part of many properties, and originator can revert to the previous state based on the saved memento information. Sometimes the internal information of some objects must be kept outside the object, but it must be read by the object itself, at which point the use of the memo can mask the complex object's internal information to other objects. Of course, the biggest effect is that when the status of a character changes, it is possible that the state is invalid, and then the state can be recovered using a memo that is stored temporarily. Well, in fact, toss and turn is to restore data use, Chice said, the following to give you a simple show the implementation of the Code bar.

Objective-c Code implementation:

Originator:

Copy Code code as follows:

Initiator: Records the internal state of the current moment, responsible for defining what is the backup scope of the state, responsible for creating and recovering the memo data.

#import <Foundation/Foundation.h>
@class Nimomemento;

@interface Nimooriginator:nsobject

@property (nonatomic, copy) nsstring* state;

-(Nimomemento *) Creatememento;
-(void) Restorememento: (Nimomemento *) memento;

@end

Copy Code code as follows:

#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:
Copy Code code as follows:

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, copy, readonly) NSString *state;
-(ID) initwithstate: (NSString *) state;

@end

Copy Code code as follows:

#import "NimoMemento.h"

@interface Nimomemento ()

@property (nonatomic, copy, ReadWrite) NSString *state;


@end

Copy Code code as follows:

@implementation Nimomemento

-(ID) initwithstate: (NSString *) state
{
if (self = [super init]) {
_state = [state copy];
}

return self;
}

@end


Caretaker:
Copy Code code as follows:

Management role: Manage memos, save and provide memos.

#import <Foundation/Foundation.h>
@class Nimomemento;

@interface Nimocaretaker:nsobject

@property (nonatomic, assign) Nimomemento *memento;

@end

Copy Code code as follows:

//
Nimocaretaker.m
Mementodemo
//

#import "NimoCaretaker.h"

@implementation Nimocaretaker

@end


Copy Code code as follows:

Client:

#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:old
2015-08-12 20:27:39.186] State: New
2015-08-12 20:27:39.186 mementodemo[1160:34914] State:old

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

Separating the State property and the Initwithstate initialization method from the primary oral file NimoMemento.h through class extensions:

Copy Code code as follows:

//
Nimomemento+private.h
Mementodemo
//

#import "NimoMemento.h"

@interface Nimomemento ()

@property (nonatomic, copy, ReadWrite) NSString *state;
-(ID) initwithstate: (NSString *) state;

@end

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

Related Article

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.