Memorandum mode:
Memento: capture the internal state of an object without compromising encapsulation, and save the state outside the object. In order to restore the object to the original save state.
Demo:
// Memorandum Mode
Class Program
{
Static void main (string [] ARGs)
{
Originator o = new originator ();
O. State = "on ";
O. Show ();
// Instantiate the manager and save the status
Caretaker c = new caretaker ();
C. mement0 = O. creatememento ();
O. State = "off ";
O. Show ();
// Restore status
O. setmemento (C. mement0 );
O. Show ();
Console. readkey ();
}
}
Class Originator
{
Private string state; // saved attributes
Public String state
{
Get {return state ;}
Set {state = value ;}
}
Public memento creatememento () // create a memorandum to import and instantiate a memento object.
{
Return (new memento (State ));
}
Public void setmemento (memento) // restore Memorandum
{
State = memento. State;
}
Public void show ()
{
Console. writeline ("state =" + State );
}
}
// Memorandum
Class memento
{
Private string state;
Public memento (string state)
{
This. State = State;
}
Public String state
{
Get {return state ;}
}
}
// Manager class
Class caretaker
{
Private memento;
Public memento mement0 // get or set the Memorandum
{
Get {return memento ;}
Set {memento = value ;}
}
}