1// Memento 2 3// Intent: "Without violating encapsulation, capture and externalize an 4// object's internal state so that an object can be restored to this 5// state later." 6 7// For further information, read "Design Patterns", p283, Gamma et al., 8// Addison-Wesley, ISBN:0-201-63361-2 9 10/**//* Notes: 11 * We often have client code that wishes to record the current state of an 12 * object, without being interested in the actual data values (this is 13 * needed for undo and checkpointing). To support this behavior, we can have 14 * the object record its internal data in a helper class called a memento, and 15 * the client code can treat this as an opaque store of the object's state. 16 * At some later point, the client can pass the memento back into the object, 17 * to restore it to the previous state. 18*/ 19 20namespace Memento_DesignPattern 21{ 22using System; 23 24class Originator 25{ 26privatedouble manufacturer=0; 27privatedouble distributor =0; 28privatedouble retailer =0; 29 30publicvoid MakeSale(double purchasePrice) 31{ 32// We assume sales are divided equally amount the three 33 manufacturer += purchasePrice * .40; 34 distributor += purchasePrice *.3; 35 retailer += purchasePrice *.3; 36// Note: to avoid rounding errors for real money handling 37// apps, we should be using decimal integers 38// (but hey, this is just a demo!) 39 } 40 41public Memento CreateMemento() 42{ 43return (new Memento(manufacturer, distributor, retailer)); 44 } 45 46publicvoid SetMemento(Memento m) 47{ 48 manufacturer = m.A; 49 distributor = m.B; 50 retailer = m.C; 51 } 52 } 53 54class Memento 55{ 56privatedouble iA; 57privatedouble iB; 58privatedouble iC; 59 60public Memento(double a, double b, double c) 61{ 62 iA = a; 63 iB = b; 64 iC = c; 65 } 66 67publicdouble A 68{ 69get 70{ 71return iA; 72 } 73 } 74 75publicdouble B 76{ 77get 78{ 79return iB; 80 } 81 } 82 83publicdouble C 84{ 85get 86{ 87return iC; 88 } 89 } 90 } 91 92class caretaker 93{ 94 95 } 96 97/**////<summary> 98/// Summary description for Client. 99///</summary> 100publicclass Client 101{ 102publicstaticint Main(string[] args) 103{ 104 Originator o =new Originator(); 105 106// Assume that during the course of running an application 107// we we set various data in the originator 108 o.MakeSale(45.0); 109 o.MakeSale(60.0); 110 111// Now we wish to record the state of the object 112 Memento m = o.CreateMemento(); 113 114// We make further changes to the object 115 o.MakeSale(60.0); 116 o.MakeSale(10.0); 117 o.MakeSale(320.0); 118 119// Then we decide ot change our minds, and revert to the saved state (and lose the changes since then) 120 o.SetMemento(m); 121 122return0; 123 } 124 } 125} 126 127