Analysis of Android source code design patterns and practices (13th)

Source: Internet
Author: User

Analysis of Android source code design patterns and practices (13th)
Chapter 2 memorandum Model

The memo mode is a behavior mode that stores the current state of an object and can be restored to this State later. It is a bit like what we usually call "regret ".

1. Definition

Capture the internal state of an object without interrupting the closed state, and save the state outside the object. In this way, the object can be restored to the previously saved state.

2. Use Cases

(1) You need to save the state or partial state of an object at a certain time point.

(2) If an interface is used to obtain these States for other objects, the implementation details of the objects will be exposed and the object encapsulation will be broken. An object does not want external access to its internal state directly, an intermediate object can indirectly access its internal status.

3. Simple implementation

Example in the book: the "Call of Duty" game is used as an example to use the archiving function in the game.

First, Memorandum

/*** Memorandum class */public class Memento {public int mCheckpoint; // weapon public int mLiftValue; // life public String mWeapon; // level @ Override public String toString () {return "Memento [mCheckpoint =" + mCheckpoint + ", mLiftValue =" + mLiftValue + ", mWeapon =" + mWeapon + "]";}

Game class. In this class, you can use the createmento function to create the user's memo object. The external function can use the restore function to restore the state of the CallOfDuty object from the memo object.

/***** Simple simulation of the "summon call" game **/public class CallOfDuty {private int mCheckpoint = 1; private int mLiftValue = 100; private String mWeapon = "Desert e "; // play the public void play () {System. out. println ("playing game:" + String. format ("% d off", mCheckpoint) + "fighting for the enemy"); mLiftValue-= 10; System. out. println ("upgraded"); mCheckpoint ++; System. out. println ("Arrive" + String. format ("% d off", mCheckpoint);} // exit the public void quit () {System. out. println ("--------------"); System. out. println ("game attributes before exiting:" + this. toString (); System. out. println ("Quit game"); System. out. println ("--------------");}/*** create memo */public Memento createMemento () {Memento memento = new Memento (); memento. mCheckpoint = mCheckpoint; memento. mLiftValue = mLiftValue; memento. mWeapon = mWeapon; return memento;} // restore the public void restore (Memento memento) {this. mCheckpoint = memento. mCheckpoint; this. mLiftValue = memento. mLiftValue; this. mWeapon = memento. mWeapon; System. out. println ("restored game attributes:" + this. toString ();} // omitting the getter and setter Methods @ Override public String toString () {return "CallOfDuty [mCheckpoint =" + mCheckpoint + ", mLiftValue = "+ mLiftValue +", mWeapon = "+ mWeapon +"] ";}}

Memorandum operation class:

/*** Caretaker, responsible for managing Memento */public class Caretaker {Memento mMemento; // Memorandum/*** archive */public void archive (Memento memento) {this. mMemento = memento;}/*** get the archive */public Memento getMemento () {return mMemento ;}}

Client code:

Public class Client {public static void main (String [] args) {// construct the game object CallOfDuty game = new CallOfDuty (); // 1. playing game. play (); Caretaker caretaker = new Caretaker (); // 2. game archive caretaker. archive (game. createMemento (); // 3. withdraw from the game. quit (); // 4. resume the game CallOfDuty newGame = new CallOfDuty (); newGame. restore (caretaker. getMemento (); // 5. play the game again (not archived. play (); // 6. archived newGame. restore (caretaker. getMemento ());}}

Result:

Playing a game: the progress of the 1st-level fight against the enemy has been upgraded to reach the 2nd-level ------------ game Attribute before exiting: CallOfDuty [mCheckpoint = 2, mLiftValue = 90, mWeapon = desert e] withdraw from the game ------------ restored game attributes: CallOfDuty [mCheckpoint = 2, mLiftValue = 90, mWeapon = desert e: the progress of the 2nd-level fight against the enemy upgraded the game attribute after the 3rd-level recovery: CallOfDuty [mCheckpoint = 2, mLiftValue = 90, mWeapon = desert e]

In the code above, each role has clear and single responsibilities, and the code is relatively simple. That is, direct access to the CallOfDuty role is blocked, while satisfying the object state access function, the structure of this module is kept clear and tidy.

4. Memo mode in Android Source Code 1. onSaveInstanceState and onRestoreInstanceState

When the Activity does not exit normally and the Activity is killed by the system in the following time, the two methods will be called to allow developers to store Activity-related information, the data will be restored the next time the Activity is returned. Use these two functions. Developers can store interface-related information in some special scenarios to improve user experience.

5. Summary 1. Advantages

(1) provides users with a recovery mechanism that allows users to easily return to a historical state.

(2) Information is encapsulated so that users do not need to care about the details of the status.

2. Disadvantages

Resource consumption. If the class has too many member variables, it will occupy a large amount of resources and consume a certain amount of memory each time it is saved.

PS: this should be the last article of this year. There are a total of 26 chapters in "Analysis and Practice of Android source code design patterns". This month is just half the better, and we will continue to work hard next month. Finally, I wish you a happy New Year and a rapid technological advances in the new year !! Thank you for your support!

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.