Detailed explanation of memo mode of Android programming design mode

This paper describes the memo mode of Android programming design mode. Share with you for your reference, as follows:

1、 Introduction

Memo mode is a behavior mode, which is used to save the current state of the object and can be restored to this state again later, which is a bit like what we usually call "regret medicine" The memo mode needs to be implemented in such a way that the saved object state cannot be accessed by the object from the outside. The purpose is to protect the integrity of the saved object state and the internal implementation from being exposed to the outside.

2、 Definition

Without breaking the closure, capture the internal state of an object and save the state outside the object, so that the object can be restored to the original saved state later.

3、 Usage scenario

You need to save the state or part of the state of an object at a certain time.

If an interface is used to let other objects get these States, it will expose the implementation details of the object and destroy the encapsulation of the object. An object does not want the outside world to directly access its internal state, and its internal state can be accessed indirectly through the intermediate object.

4、 UML class diagram of memo pattern

UML class diagram:

Role introduction:

Originator: responsible for creating a memo, which can record and restore its internal state. At the same time, the originator can also decide which internal states memento stores as needed.

Memento: a memo role, which is used to store the internal state of the originator and prevent objects other than the originator from accessing memento.

Caretaker: it is responsible for storing memos. It cannot operate or access the contents of memos. It can only pass memos to other objects.

5、 Simple example

For the memo mode, the more appropriate scene should be the archiving function in the game. This function is to store the game progress in the local file system or database, and load the progress locally when entering again next time, so that players can continue the last game trip. Here we call on duty "Take this game as an example to briefly demonstrate the implementation of memo mode.

First, we create game class, memo class and caretaker class, play the game to a node, archive the game, then exit the game, read from the archive when re entering, and the progress when entering the archive.

Games:

In the Callofduty game class, we store several key fields, such as level, character's life value and weapon. When we call the play function to play the game, we modify the level and character's life value. In this class, you can use the creatememo function to create the user's memo object, that is, save its own state to a memo object. External users can restore the state of the Callofduty object from the memo object through the restore function.

Let's take a look at the memo object. It only stores the field of Callofduty object. The specific code is as follows:

Memos:

This is a stateless and no operation entity class. It is only used to store some data of the originator role and prevent external direct access to the originator.

The operator of the memo is caretaker. Let's look at the relevant codes:

Caretaker class:

The responsibility of the caretaker class is very simple. It is responsible for managing memo objects, that is, memo objects.

Client class:

result:

The above process roughly includes the following four steps:

(1) Start the game and upgrade; (2) Archive before exiting the game; (3) Exit the game; (4) Restart the game and restore game progress from the archive.

Callofduty here is the originator role, that is, the object that needs to store data. Instead of directly storing the Callofduty object, the Callofduty object's data is stored through memoto, and then the memoto object is stored. Finally, the access operation of memoto is handed over to the caretaker object. In this process, the responsibilities of each role are clear and single, and the code is relatively simple, that is, the direct access to the Callofduty role is shielded, which not only meets the object state access function, but also keeps the structure of the module clear and tidy.

6、 Memo mode in Android source code

1. Onsaveinstancestate and onrestoreinstancestate

When the activity exits in an abnormal way and the activity is killed by the system in a subsequent time, these two methods will be called, so that developers can have the opportunity to store activity related information and recover these data the next time they return to the activity. Through these two functions. Developers can store interface related information in some special scenarios to improve the user experience.

7、 Summary

The memo mode is to store a snapshot of the internal state of another object through the memo object without destroying the encapsulation, and restore the object to the stored state at an appropriate time in the future.

advantage:

It provides users with a mechanism to restore the state, which can make it easier for users to return to a historical state.

The encapsulation of information is realized, so that users do not need to care about the details of state preservation.

Disadvantages:

Resource consumption. If there are too many member variables of a class, it is bound to occupy large resources, and each save will consume a certain amount of memory.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>