Memo Mode (Memento), which captures the internal state of an object without compromising encapsulation, and saves the state outside of that object. The object can then be restored to its previously saved state.
Originator (INITIATOR): Responsible for creating a memo memento that records its internal state at the current moment and can use the memo to restore its internal state. Originator can determine which internal states of the memento storage originator as needed.
Memento (Memo): is responsible for storing the internal state of the originator object and prevents objects other than originator from accessing the memo Memento. The memo has two interfaces, and caretaker can only see the narrow interface of the memo, which can only pass the memo to other objects. Originator can see a wide interface that allows it to access all the data needed to return to the previous state.
Caretaker (manager): Responsible for the preservation of the memorandum Memento, the contents of the memo can not be manipulated or checked.
Memo mode disadvantage: The role state needs to be fully stored in the memo object, and if the state data is much larger, the memo object is very memory-intensive on resource consumption.
Demo1:
classOriginator {Private stringState ; Public stringState {Get{returnState ;} Set{state =value;} } PublicMemento Creatememento () {Console.WriteLine ("Create a Memo"); return NewMemento (state); } Public voidSetmemento (Memento Memento) {Console.WriteLine ("Storage Memo"); State=Memento. State; } Public voidShowstate () {Console.WriteLine ("state={0}", state); } }
class Memento { privatestring state ; Public string State { getreturn state ;} } Public Memento (string State ) { the. State = State ; } }
class Caretaker { private Memento Memento; Public Memento Memento { getreturn Memento;} Set {memento = value;} } }
Test:
New originator (); " on " ; O.showstate (); New caretaker (); = O.creatememento (); " Off " ; O.showstate (); O.setmemento (C.memento); O.showstate ();
Notes-Liar design mode-18 Memo Mode