Document directory
- 1. Memento pattern (memorandum Mode)
- Ii. UML class diagram
- Iii. Memento pattern (memorandum mode) instance code
- 4. instance code in memorandum Mode
Http://www.dofactory.com/Patterns/PatternMemento.aspx.
1. Memento pattern (memorandum Mode)
Define: without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this State later.
Definition: capture and externalize the internal state of an object without violating encapsulation, so that this object can be stored after this state.
Ii. UML class diagram
Memento(Memento)
- Stores internal state of the originator object. The memento may store as much or as little of the originator's internal state as necessary at its originator's discretion.
- Protect against access by objects of other than the originator. mementos have extends tively two interfaces. caretaker sees a narrow interface to the memento -- it can only pass the memento to the other objects. originator, in contrast, sees a wide interface, one that lets it access all the data necessary to restore itself to its previous state. ideally, only the originator that produces the memento wocould be permitted to access the memento's internal state.
Originator(Salesprospect)
- Creates a memento containing a snapshot of its current internal state.
- Uses the memento to restore its internal state
Caretaker(Caretaker)
Iii. Memento pattern (memorandum mode) instance code
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace memento_pattern <br/> {<br/> // <summary> </P> <p> // mainapp startup class for structural </P> <p> // memento design pattern. </P> <p> // </Summary> </P> <p> class mainapp <br/>{</P> <p> // <Summary> </P> <p> // entry point into console application. </P> <p> // </Summary> </P> <p> static void main () <br/>{</P> <p> originator o = new originator (); </P> <p> O. state = "on"; </P> <p> // store internal state </P> <p> caretaker c = new caretaker (); </P> <p> C. memento = O. creatememento (); </P> <p> // continue changing originator </P> <p> O. state = "off"; </P> <p> // restore saved state </P> <p> O. setmemento (C. memento); </P> <p> // wait for user </P> <p> console. readkey (); </P> <p >}</P> <p> // <summary> </P> <p> // 'originator' class </P> <p> // </Summary> </P> <p> class originator <br/>{</P> <p> private string _ state; </P> <p> // property </P> <p> Public String state <br/>{</P> <p> get {return _ state ;} </P> <p> set <br/> {</P> <p> _ state = value; </P> <p> console. writeline ("state =" + _ State ); </P> <p >}</P> <p> // creates memento </P> <p> Public memento creatememento () <br/>{</P> <p> return (new memento (_ State )); </P> <p >}</P> <p> // restores Original State </P> <p> Public void setmemento (memento) <br/>{</P> <p> console. writeline ("restoring State... "); </P> <p> state = memento. state; </P> <p >}</P> <p> // <summary> </P> <p> // 'memento' class </P> <p> // </Summary> </P> <p> class memento <br/>{</P> <p> private string _ state; </P> <p> // constructor </P> <p> Public memento (string state) <br/>{</P> <p> This. _ state = State; </P> <p >}</P> <p> // gets or sets state </P> <p> Public String state <br/>{</P> <p> get {return _ state ;} </P> <p >}</P> <p> // <summary> </P> <p> // 'caretaker 'class </P> <p> // </Summary> </P> <p> class caretaker <br/>{</P> <p> private memento _ memento; </P> <p> // gets or sets memento </P> <p> Public memento <br/>{</P> <p> set {_ memento = value ;} </P> <p> get {return _ memento ;}</P> <p >}< br/>
4. instance code in memorandum Mode
This real-world code demonstrates the memento pattern which temporarily saves and then restores the salesprospect's internal state.
Temporarily save and restore the internal status of salesprospect
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace memento_pattern <br/> {<br/> // <summary> </P> <p> // mainapp startup class for real-time- world </P> <p> // memento design pattern. </P> <p> // </Summary> </P> <p> class mainapp <br/>{</P> <p> // <Summary> </P> <p> // entry point into console application. </P> <p> // </Summary> </P> <p> static void main () <br/>{</P> <p> salesprospect S = new salesprospect (); </P> <p> S. name = "Noel Van Halen"; </P> <p> S. phone = "(412) 256-0990"; </P> <p> S. budget = 25000.0; </P> <p> // store internal state </P> <p> prospectmemory M = new prospectmemory (); </P> <p> M. memento = S. savememento (); </P> <p> // continue changing originator </P> <p> S. name = "Leo Welch"; </P> <p> S. phone = "(310) 209-7111"; </P> <p> S. budget = 1000000.0; </P> <p> // restore saved state </P> <p> S. restorememento (M. memento); </P> <p> // wait for user </P> <p> console. readkey (); </P> <p >}</P> <p> // <summary> </P> <p> // 'originator' class </P> <p> // </Summary> </P> <p> class salesprospect <br/>{</P> <p> private string _ name; </P> <p> private string _ phone; </P> <p> private double _ budget; </P> <p> // gets or sets name </P> <p> Public string name <br/> {</P> <p> get {return _ name ;} </P> <p> set <br/> {</P> <p> _ name = value; </P> <p> console. writeline ("name:" + _ name ); </P> <p >}</P> <p> // gets or sets phone </P> <p> Public String phone <BR/>{</P> <p> get {return _ phone ;} </P> <p> set <br/> {</P> <p> _ phone = value; </P> <p> console. writeline ("Phone:" + _ phone ); </P> <p >}</P> <p> // gets or sets budget </P> <p> Public double budget <BR/>{</P> <p> get {return _ budget ;} </P> <p> set <br/> {</P> <p> _ budget = value; </P> <p> console. writeline ("budget:" + _ budget ); </P> <p >}</P> <p> // stores memento </P> <p> Public memento savemento () <br/>{</P> <p> console. writeline ("/nsaving state --/N"); </P> <p> return new memento (_ name, _ phone, _ budget ); </P> <p >}</P> <p> // restores memento </P> <p> Public void restoremento (memento) <br/>{</P> <p> console. writeline ("/nrestoring state --/N"); </P> <p> This. name = memento. name; </P> <p> This. phone = memento. phone; </P> <p> This. budget = memento. budget; </P> <p >}</P> <p> // <summary> </P> <p> // 'memento' class </P> <p> // </Summary> </P> <p> class memento <br/>{</P> <p> private string _ name; </P> <p> private string _ phone; </P> <p> private double _ budget; </P> <p> // constructor </P> <p> Public memento (string name, string phone, double budget) <br/>{</P> <p> This. _ name = Name; </P> <p> This. _ phone = phone; </P> <p> This. _ budget = budget; </P> <p >}</P> <p> // gets or sets name </P> <p> Public string name <br/>{</P> <p> get {return _ name ;} </P> <p> set {_ name = value ;} </P> <p >}</P> <p> // gets or set phone </P> <p> Public String phone <br/> {</P> <p> get {return _ phone ;} </P> <p> set {_ phone = value ;} </P> <p >}</P> <p> // gets or sets budget </P> <p> Public double budget <br/> {</P> <p> get {return _ budget ;} </P> <p> set {_ budget = value ;} </P> <p >}</P> <p> // <summary> </P> <p> // 'caretaker 'class </P> <p> // </Summary> </P> <p> class prospectmemory <br/>{</P> <p> private memento _ memento; </P> <p> // property </P> <p> Public memento <br/>{</P> <p> set {_ memento = value ;} </P> <p> get {return _ memento ;}</P> <p >}< br/>