The application of Memo mode in OBJECTIVE-C design pattern programming _ios

Source: Internet
Author: User
Tags touch

Basic understanding
This model has three key roles: primary (originator), Memo (Memento), caretaker (caretaker). The basic relationship between the three is that the original creator creates a memo containing its status and passes it on to the caretaker. The caretaker does not know how to interact with the memo, but keeps the memo in a safe place for safekeeping.
Memo (Memento): captures the internal state of an object without damaging the encapsulation, and saves the state outside the object. You can then revert the object to the previously saved state.
Originator (INITIATOR): is responsible for creating a memo to record its internal state at the current moment, and to use the recovery memo internal state. Originator can determine which internal state of the memento storage originator as needed.
Memento (Memo): is responsible for storing the internal state of the originator object and prevents access to the memo to objects other than originator. Memo has two interfaces, caretaker
Only the narrow interface of the memo can be seen, it can only pass the memo to other objects. Originator can see a wide interface that allows it to access all the data needed to return to the previous state.
Caretaker (manager): is responsible for the preservation of the memorandum, the contents of the memo can not be operated or inspected.
is to save the details to be encapsulated in the memento, which day to change the saved details will not affect the client.

The occasion of the memo use
Memo mode is useful for classes that are more complex in functionality, but need to maintain or record the history of a property, or if a property that needs to be saved is only a small part of many properties, Orignator can revert to the previous state based on the saved memento information.
If you need to implement the command revocation function when using command mode in a system, the command mode can use Memo mode to store the status of the undo operation. Sometimes the internal information of some objects must be kept outside the object, but 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.
The interface used to get the state exposes the implementation interface and needs to be masked.
It is generally used in the design of games, word processing programs that need to keep a snapshot of the complex state of the current context and revert to processing later.

Role
when the status of a character changes, it is possible that the state is invalid, and then the state can be recovered using a temporarily stored memo.
Memo mode in the Cocoa touch frame
The Cocoa Touch Framework uses memo mode for archiving, attribute list serialization, and core data.
Cocoa archiving is the encoding of objects and their properties, as well as relationships with other objects, to form a document that can be stored both in the file system and between processes or networks. The relationship between objects and other objects is considered a network of object graphs.
The archive process saves the object as a schema-independent byte stream, preserving the identity of the object and the relationship between the objects. The type of the object is also saved with the data. Objects decoded from a byte stream are typically used to instantiate the same class when the object is encoded. Using Nscoder's specific classes of nskeyedarchiver and Nskeyedunarchiver, the object encoded and decoded must comply with the Nscoding protocol and implement the following methods using the key-based archiving technology:

Copy Code code as follows:

-(ID) Initwithcoder: (Nscoder *) coder;
-(void) Encodewithcoder: (Nscoder *) coder;

Instance
Add the following two methods to the VIEWCONTROLLER.M file:

Copy Code code as follows:

-(void) savecurrentstate
{
When the user exits the application and then turns it back on, he wants to be in the same state as he did when he quit.
When you quit the app, what we need to do is store the albums that are currently displayed.
Because only a small piece of information, we can use Nsuserdefaults to store information
[[Nsuserdefaults Standarduserdefaults] setinteger:currentalbumindex forkey:@ "Currentalbumindex"];
}

-(void) loadpreviousstate
{
Currentalbumindex = [[Nsuserdefaults standarduserdefaults] integerforkey@ "Currentalbumindex"];
[Self showdataforalbumatindex:currentalbumindex];
}


Savecurrentstate stores the index of the current album to Nsuserdefaults─nsuserdefaults is a standard data store that IOS uses to specifically store program settings and data.

Loadpreviousstate loads the album index that was previously stored. This is not all of the memo mode, but you have achieved your goal.

Now, in VIEWCONTROLLER.M, before the scrolling view initializes, add the following line to the Viewdidload:

Copy Code code as follows:

[Self loadpreviousstate];

Loads the state of the last store when the program starts. But where do you store the current state of the program? You need to use the notice to do such a thing. When the program goes backstage, IOS sends a uiapplicationdidenterbackgroundnotification notification. You can use this notification to invoke savecurrentstate. Is that convenient?

Add the following line to the Viewdidload: Last Face

Copy Code code as follows:

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (savecurrentstate) Name: Uiapplicationdidenterbackgroundnotification Object:nil];

Now, when your app goes backstage, Viewcontroller automatically invokes savecurrentstate to store the current state.

Now, add the following code:

Copy Code code as follows:

-(void) dealloc
{
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
}

This is to ensure that when the Viewcontroller is released, the Observer of the class is removed.

Build and run your app, click on an album, run the program in the background with command+shift+h (if you're using an emulator), and then turn off the app. Restart the app and check to see if the previously selected album is centered:

The album data looks right, but the correct album cover is not centered, what's wrong?

This is the use of alternative methods Initialviewindexforhorizontalscroller! Because this method is not commissioned, Viewcontroller will always display the default first album cover in this case.

To fix the problem, add the following code to the VIEWCONTROLLER.M:

Copy Code code as follows:

-(Nsinteger) Initialviewindexforhorizontalscroller: (Horizontalscroller *) scroller
{
return currentalbumindex;
}

The first view of Horizontalscroller is now always set to a currentalbumindex indexed picture. This approach ensures that your app has a great user experience and that it is recoverable.

Rerun your app, roll album cover, close app, and reboot to make sure the problem is resolved:

If you look at the Persistencymanger initialization method, you will notice that the album's data is a hard-coded one, and that the data will be created again Persistencymanger each time it is created. Is there a better way to store album lists when they are created? So how do you store the album data in a file?

One option is to iterate over the properties of the Album and store it in a plist file, recreating a Album instance when they are needed. This is not the best choice, which requires you to write specific code based on different data or attributes in each class. example, if you later need a movie class that has a few different attributes, you need to write some new code to store and load the data.

In addition, you cannot store private variables in each instance of a class, because they are inaccessible external classes. That's why Apple wants to create an archiving (archiving) mechanism.

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.