iOS design mode-Memo design mode and command design mode

Source: Internet
Author: User

What is a memo mode?

In response to certain events, the application needs to save its own 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 state of the current session, such as the level of the game, the number of enemies, the types of weapons available, etc. Once the game is opened again, players can play from where they left off. Many times, saving the state of a program really does not require a particularly ingenious method. 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 holds. This is how the memo pattern is used in software design for games, word processing, and other programs that need to save 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 package, 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 of it) at a certain point in time so that you can revert back to the previous state.

@: The interface used to get the state will miss the details of the implementation and need to be hidden.

Memo mode in the Cocoa touch frame

The Cocoa Touch Framework uses a memo pattern in archiving, attribute list serialization, and core data. The archive for cocoa 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 an object and other objects is seen as a network of object graphs. The archive process saves the object as a schema-independent stream of bytes, 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 when the object is encoded.

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 the program is usually 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. Nscoder itself is an abstract class. Apple recommends using key-based archiving techniques through Nscoder specific classes of nskeyarchiver and Nskeyedunarchiver. The object being encoded and decoded must adhere to the Nscoding protocol and implement the following methods:

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


What is command mode?

The Command object encapsulates information about how commands are executed against the target, so the client or caller does not have to know any details of the target, but can still perform any existing operations on it. By encapsulating the request into an object, the client can parameterize and place the queue or log, and can support the revocable operation. The command object binds one or more actions to a specific sink. The command pattern eliminates the binding between the action as an object and the receiver that executes it.

Command mode: Encapsulates a request as an object, which allows the client to be parameterized with different requests, to request a queue or to record a request log, and to support revocable operations.

When to use command mode?

It is natural to consider using this pattern in the following situations.

@: You want your app to support undo and redo.

@: You want to parameterize an action with an object to perform an action and use a different command object instead of a callback function.

@: You want to specify, arrange, and execute requests at different times.

@: You want to record the change log so that in the case of a system failure, these changes can be done again later.

@: To allow the system to support transactions, transactions encapsulate a series of modifications to the data, and transactions can be modeled as command objects.

Use the command mode in the Cocoa Touch framework:

The Nsinvocation, nsundomanage, and target-action mechanisms are typical references to this pattern in the framework.


How to use Memo mode and command mode, see this article below:

http://blog.jobbole.com/48179/(translated) Http://www.raywenderlich.com/46988/ios-design-patterns (original)

Demo link address for this article:

https://[email protected]/guoshimeihua/memoryandcommanddemo.git

This article notes the pattern of memos and the command pattern as written here.

iOS design mode-Memo design mode and command design 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.