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