Application example of Memo mode in design pattern development in IOS applications development _ios

Source: Internet
Author: User

What is Memo mode?

In response to certain events, the application needs to save its state, such as when the user saves the document or the program exits. For example, before the game exits, you may need to save the status of the current session, such as the game level, the number of enemies, the type of weapon available, and so on. When the game is opened again, the player can play from where he left off. Most of the time, saving the state of the program really does not require a particularly ingenious approach. Any simple and effective method can be, but at the same time, saving information should only be meaningful to the original program. The original program should be the only entity that can decode the information in the document it is saving. This is how memo mode is used in software design for programs such as games, word processing, and so on, which need to keep a snapshot of the complex state of the current context and restore it later.

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

When do I use Memo mode?

You should consider using this pattern when the following two conditions are met:

@: You need to save the state of an object (or part) at a certain point in time so that you can revert back to the previous state.

@: The interface used to get the state will leak the details of implementation and need to hide it.

How to use Memo mode:
Add the following method to the VIEWCONTROLLER.M:

Copy Code code as follows:

-(void) savecurrentstate

{

When the user leaves the app and then comes back again, he wants it to being in the exact of the state

He left it. In order to do this we need to save the currently displayed album.

Since It's only one piece of the information we can use Nsuserdefaults.

[[Nsuserdefaultsstandarduserdefaults] Setinteger:currentalbumindex forkey:@ "Currentalbumindex"];

}

-(void) loadpreviousstate

{

Currentalbumindex = [[Nsuserdefaultsstandarduserdefaults] integerforkey:@ "Currentalbumindex"];

[Self showdataforalbumatindex:currentalbumindex];

}

Savecurrentstate saves the current album Index to Nsuserdefaults,nsuserdefaults is the place where iOS provides information and data for saving application settings.
Loadpreviousstate the index saved before loading. This is not really a complete implementation of the memo mode, but you already know it.
Now, in the Viewcontroller.m Viewdidload method, add the following code before the Scroller initialization:
Copy Code code as follows:

[Self loadpreviousstate];

It will load the previously saved state when the application starts. But when do you want to save the status of the application? You will use notifications to implement it. When the application is in the background, iOS will send uiapplicationdidenterbackgroundnotification notification, you can use this notification to save the state, this is not very convenient?
Add the following code to the Viewdidload:
Copy Code code as follows:

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

Now, when the application goes backstage, Viewcontroller will automatically save the current state through the Savecurrentstate method.
Now add the following code:
Copy Code code as follows:

-(void) dealloc

{

[[Nsnotificationcenterdefaultcenter] removeobserver:self];

}

This will ensure that the observer is removed when the Viewcontroller is destroyed.
Build and run your application, navigate to an album, then send the app to the background via command+shift+h (emulator), and then close the app. Once again, open the app and check to see if the original selected album was shown in the middle:

It looks like the album data is correct, but the middle view doesn't show the correct album. What's the situation? This is the purpose of the optional method Initialviewindexforhorizontalscroller. Because this method is not implemented in a delegate, this is always the first view of the initialization view.
To fix this problem, add the following code to the VIEWCONTROLLER.M:

Copy Code code as follows:

-(Nsinteger) Initialviewindexforhorizontalscroller: (Horizontalscroller *) scroller

{

return currentalbumindex;

}

Now the first view of Horizontalscroller is finally set to currentalbumindex the specified view. This allows the app to retain its last-used status the next time it is used.
Run your app again, roll the album as before, stop the application, reboot, and make sure the problem is fixed:

If you look at the Persistencymanager init method, you'll notice that the album data is hard-coded and recreated each time. But the better Way is to create album lists once and then store them into a file, how do you save the album data to a file?
An optional Way is to loop album properties, save them to a plist file, and reconstruct them when they are needed. This is not the best way because you need to write specific code that is associated with the attributes of each class. For example, if you're going to create a movie class with different attributes, the saved and loaded code needs to be written again.
In addition, you cannot save the private variables for each class because they are not visible in the outer class. This is why Apple created the archiving (archiving) mechanism.

Memo mode in the Cocoa touch frame

The Cocoa Touch Framework uses a memo pattern in Archive, attribute list serialization, and core data. Cocoa archiving is the encoding of objects and their attributes, as well as relationships with other objects, to form a document that can be stored either in the file system or 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 instantiated with the same type as the object encoding.
If you want to archive an object, many times we consider saving the state of the program. In the model-view-controller paradigm, the state of a program is typically maintained by the model object. We encode the model object into the document and then decode it and read it back. Encode and decode operations using the Nscoder object at run time. The Nscoder itself is an abstract class. Apple recommends the use of key-based archiving techniques through Nscoder specific classes of nskeyarchiver and Nskeyedunarchiver. Objects that are encoded and decoded must comply with the Nscoding protocol and implement the following methods:

Copy Code code as follows:

-(void) Encodewithcoder: (Nscoder *) Acoder;
-(ID) Initwithcoder: (Nscoder *) Adecoder;

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.