[Go] design mode in Android-Memo mode

Source: Internet
Author: User

Switch to design mode in Android-Memo mode

Defined

The definition of the memo design pattern is to entrust the State records and management of the object to the outside world to maintain its own closeness.

Compare the official definition

The memo pattern (Memento pattern), also known as snapshot mode (Snapshot pattern) or token mode, is one of the 23 design patterns of Gof, which belongs to the behavioral pattern.

Definition: captures the internal state of an object without destroying it, and saves the state outside that object. The object can then be restored to its previously saved state.

Role
    • Notebooks: Many of the internal states need to be created with a memo to manage, create and remove memos to restore status.

    • Memo classes: Entity classes are used to record the state of a notebook

    • Memo Management classes: for managing memos, storing and fetching.

Advantages and disadvantages of the memo model
    • Using the memo mode, the complex initiator's internal information can be shielded from other objects, so that the encapsulated boundaries can be properly maintained.

    • This model simplifies the initiation of human beings. The initiator no longer needs to manage and save a version of its internal state, and the client can manage the versions of those states they need.

    • When the status of the initiator is changed, it is possible that the state is invalid, and then the state can be restored using a temporarily stored memo.

Ii. shortcomings of the memo model:
    • If the status of the initiator role needs to be stored completely in the memo object, the memo object can be expensive on resource consumption.

    • When the responsible role stores a memo, the person in charge may not know how much storage space the state consumes, and thus cannot alert the user whether an operation is expensive.

    • When the status of the initiator role changes, it is possible that the protocol is invalid. If the success rate of state change is not high, it is better to take the "if" protocol mode.

Typical use in Android

State saving is a typical use of the memo mode in Android, which corresponds to two callback methods for activity Onsaveinstancestate () and Onrestoreinstancestate ()

Role correspondence
    • Bundle Correspondence Memo: The state of Android, including the View tree state and fragment state, and the life cycle state are saved by the parcel object that stores the key value pair through the bundle, especially note that The view of the same activity is placed in a bundle with Sparcearray (similar to HashMap, but the space is more efficient, the internal lookup dichotomy, and the keys can only be integers) to store. Key: ViewId. Value: The corresponding Parcel object, so viewid cannot be duplicated, otherwise it will overwrite.

    • Activity corresponds to the memo management class. Strictly speaking, it should be an intrinsic property within the activity. Mactivity is actually a Activityclientrecord collection, each activity information corresponds to a activityclientrecord, the corresponding key is token. The Activityclientrecord bundle type state corresponding to the bundle (memo).

    • The view and fragment, etc. all correspond to the notebook class, They all need to accompany the activity's life cycle functions onsaveinstancestate () and Onrestoreinstancestate () to complete their state management through the data structure of the bundle.

By the way: State saving and view drawing as well as event response. Is through the delegated responsibility chain design pattern, is a U-shaped structure, the next chapter will talk about the android inside the responsibility chain design mode.

If there is any mistake, please feel free to communicate.

[Go] design mode in Android-Memo 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.