"Android source design mode analysis and actual combat" reading notes (13)

Source: Internet
Author: User

13th chapter, Memorandum mode

The memo pattern is a behavior pattern that holds the current state of the object and can then revert back to this state again, somewhat like the "regret pill" we normally call it.

1. Definition

The internal state of an object is captured without breaking the closure, and the state is saved outside the object, so that the object can be restored to its previously saved state.

2. Usage Scenarios

(1) Need to save the state or part state of an object at a certain point in time.

(2) If an interface is used to get these states for other objects, the object's implementation details are exposed and the object's encapsulation is destroyed, an object does not want the outside world to directly access its internal state, and the intermediate object can indirectly access its internal state.

3. Simple implementation

Example: Take the call of Duty game as an example, using the game's archive function.

First, the memo class.

/** * Memo class * * Public  class Memento {     Public intMcheckpoint;//Weapons     Public intMliftvalue;//Life     PublicString Mweapon;//Level    @Override     PublicStringtoString() {return "Memento [mcheckpoint="+ Mcheckpoint +", mliftvalue="+ Mliftvalue +", mweapon="+ Mweapon +"]"; }}

A game class in which a Creatememento function can be used to create a memo object for the user, which can be externally recovered from the memo object through the Restore function of the CallofDuty object.

/** * * Simple analog "Call of Duty" game * */ Public  class callofduty {    Private intMcheckpoint =1;Private intMliftvalue = -;PrivateString Mweapon ="The Eagle of the desert";//Play games     Public void Play() {System.out.println ("Play the Game:"+string.format ("section%d", Mcheckpoint) +"Fighting the enemy."); Mliftvalue-=Ten; System.out.println ("Progress has been upgraded");        mcheckpoint++; System.out.println ("Arrival"+ String.Format ("section%d", Mcheckpoint)); }//exit the game     Public void quit() {System.out.println ("--------------"); System.out.println ("Game properties before exiting:"+ This. toString ()); System.out.println ("Exit Game"); System.out.println ("--------------"); }/** * Create a memo * *     PublicMementoCreatememento() {Memento Memento =NewMemento ();        Memento.mcheckpoint = Mcheckpoint;        Memento.mliftvalue = Mliftvalue; Memento.mweapon = Mweapon;returnMemento }//Recovery game     Public void Restore(Memento Memento) { This. Mcheckpoint = Memento.mcheckpoint; This. Mliftvalue = Memento.mliftvalue; This. mweapon = Memento.mweapon; System.out.println ("Game Properties after recovery:"+ This. toString ()); }//Omit getter and Setter methods    @Override     PublicStringtoString() {return "CallofDuty [mcheckpoint="+ Mcheckpoint +", mliftvalue="+ Mliftvalue +", mweapon="+ Mweapon +"]"; }}

Memo Action class:

/** * Caretaker,负责管理Memento */publicclass Caretaker {    //备忘录    /**     * 存档     */    publicvoidarchive(Memento memento){        this.mMemento = memento;    }    /**     * 获取存档     */    publicgetMemento(){        return mMemento;    }}

The client uses code:

 Public classClient { Public Static void Main(string[] args) {//Build Game ObjectsCallofDuty game =NewCallofDuty ();//1. Playing gamesGame.play (); Caretaker caretaker =NewCaretaker ();//2. Game ArchiveCaretaker.archive (Game.creatememento ());//3. Exiting the gameGame.quit ();//4. Resuming gamesCallofDuty Newgame =NewCallofDuty (); Newgame.restore (Caretaker.getmemento ());//5. Play the game again (do not archive)Game.play ();//6. Archive before RecoveryNewgame.restore (Caretaker.getmemento ()); }}


打游戏:第1关奋战杀敌中进度升级了到达第2关--------------退出前的游戏属性:CallOfDuty [mCheckpoint=2,mLiftValue=90,mWeapon=沙漠之鹰]退出游戏--------------恢复后的游戏属性:CallOfDuty [mCheckpoint=2,mLiftValue=90,mWeapon=沙漠之鹰]打游戏:第2关奋战杀敌中进度升级了到达第3关恢复后的游戏属性:CallOfDuty [mCheckpoint=2,mLiftValue=90,mWeapon=沙漠之鹰]

In the above code, each role is clear, single, and the code is relatively simple, that is, to shield the direct access to the callofduty role, in satisfying the object state access function, but also make the structure of the module remains clear and tidy.

Memo mode 1.onSaveInstanceState and onrestoreinstancestate in 4.Android source code

These two methods are called when activity is not normally exited and the activity is killed by the system in the subsequent time, giving developers the opportunity to store activity-related information and recover the data the next time they return to activity. Through these two functions. Developers can store interface-related information in special scenarios to enhance the user experience.

Basic Use Reference: Links

5. Summary 1. Advantages

(1) To provide users with a mechanism to restore the state, you can make it easier for users to return to a historical state.

(2) To achieve the encapsulation of information, so that users do not need to care about the state of preservation details.

2. Disadvantages

Consumes resources, if the class has too many member variables, it is bound to occupy a larger resource, and each time the save will consume a certain amount of memory.

PS: This should be the last article of this year, "Android source design mode analysis and actual combat" altogether 26 chapters, this month just see half, continue to work hard next month. Finally, I wish you a happy New Year's Day, and the technology is advancing rapidly!! Finally, thank you for your support, that's it!

"Android source design mode analysis and actual combat" reading notes (13)

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.