Design Pattern---Memo mode

Source: Internet
Author: User

Basic definition

Memo mode: Captures the internal state of an object without compromising encapsulation, and saves the state outside that object. The object can then be restored to its previously saved state.

Explanation: Simply put, you can save an object in a particular state at a certain point in time and restore the object to its saved state at a later time.

Basic code:

classOriginator {Private stringState ;  Public stringState {Get{returnState ;} Set{state =value;} }         PublicMemento Creatememento () {return NewMemento (state); }         Public voidSetmemento (Memento Memento) { state=Memento.        State; }         Public voidDisplay () {Console.WriteLine ("state="+State ); }    }    classMemento {Private stringState ;  Public stringState {Get{returnState ;} }         PublicMemento (stringState ) {             This. State =State ; }    }    classCaretaker {PrivateMemento memento1;  PublicMemento Memento1 {Get{returnmemento1;} Set{memento1 =value;} }    }
classOriginator {Private stringState ;  Public stringState {Get{returnState ;} Set{state =value;} }         PublicMemento Creatememento () {return NewMemento (state); }         Public voidSetmemento (Memento Memento) { state=Memento.        State; }         Public voidDisplay () {Console.WriteLine ("state="+State ); }    }    classMemento {Private stringState ;  Public stringState {Get{returnState ;} }         PublicMemento (stringState ) {             This. State =State ; }    }    classCaretaker {PrivateMemento memento1;  PublicMemento Memento1 {Get{returnmemento1;} Set{memento1 =value;} }    }

Client calls:

New originator ();             "  on " ;            Originator. Display ();             New caretaker ();             = originator. Creatememento ();             " off " ;            Originator. Display ();            Originator. Setmemento (caretaker. MEMENTO1);            Originator. Display ();
View Code

List specific examples:

Play the game, save a character in the state before hitting the boss, to hit the boss after the end of the state before returning to it.

classProgram {Static voidMain (string[] args) {Role Role=NewRole ( -, -); Console.WriteLine ("before the war:"); Role.            Display (); Caretaker caretaker=Newcaretaker (); Caretaker.memento=role.            Savememento (); Role. Setbloodflow ( -); Role. Setmagicpoint ( -); Console.WriteLine ("War"); Role.            Display (); Role. Restorememento (caretaker.            Getmemento ()); Console.WriteLine ("Recovery"); Role.            Display ();        Console.read (); }    }    classRole {Private intBloodflow; Private intMagicPoint;  PublicRole (intBloodflow,intMagicPoint) {             This. Bloodflow =Bloodflow;  This. MagicPoint =MagicPoint; }         Public intGetbloodflow () {returnBloodflow; }         Public voidSetbloodflow (intbloodflow) {             This. Bloodflow =Bloodflow; }         Public intGetmagicpoint () {returnMagicPoint; }         Public voidSetmagicpoint (intMagicPoint) {             This. MagicPoint =MagicPoint; }         Public voidDisplay () {Console.WriteLine ("user's current status:"); Console.WriteLine ("Blood Volume:"+ getbloodflow () +"; Blue Quantity:"+getmagicpoint ()); }         PublicMemento Savememento () {return NewMemento (Getbloodflow (), Getmagicpoint ()); }         Public voidRestorememento (Memento Memento) { This. Bloodflow =Memento.            Getbloodflow ();  This. MagicPoint =Memento.        Getmagicpoint (); }    }    classMemento {Private intBloodflow; Private intMagicPoint;  PublicMemento (intBloodflow,intMagicPoint) {             This. Bloodflow =Bloodflow;  This. MagicPoint =MagicPoint; }         Public intGetbloodflow () {returnBloodflow; }         Public voidSetbloodflow (intbloodflow) {             This. Bloodflow =Bloodflow; }         Public intGetmagicpoint () {returnMagicPoint; }         Public voidSetmagicpoint (intMagicPoint) {             This. MagicPoint =MagicPoint; }    }    classCaretaker { PublicMemento Memento;  PublicMemento Getmemento () {returnMemento; }         Public voidSetmemento (Memento Memento) { This. Memento =Memento; }    }
View Code

The advantages and disadvantages of Memo mode and its application scenario:

Advantages:

Provide users with a mechanism to restore the state, the user is more convenient to revert to a historical state.

Hide recovery details, the user does not need to care about the status of the save details.

Disadvantages:

The object state needs to be fully stored in the memo object, and if the state data is large, the memo object is very memory-intensive on resource consumption.

Applicable scenarios:

In short, the memo pattern can be used where the advantages of the show are avoided.

Design Pattern---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.