Computer Science Design Patterns/Memento

{{Computer Science Design Patterns/Page
 * mode={{{mode|reading}}}
 * page={{{page|{{SUBPAGENAME}}}}}
 * stage=25
 * stage date=22 May 2013
 * stage comment=Add more illustrations.
 * previous=Mediator
 * next=Model–view–controller
 * content=

Briefly, the Originator (the object to be saved) creates a snap-shot of itself as a Memento object, and passes that reference to the Caretaker object. The Caretaker object keeps the Memento until such a time as the Originator may want to revert to a previous state as recorded in the Memento object.

{{Java/Hidden begin|title=Implementation in Java}} The following Java program illustrates the "undo" usage of the Memento Pattern.

The output is: Originator: Setting state to State1 Originator: Setting state to State2 Originator: Saving to Memento. Originator: Setting state to State3 Originator: Saving to Memento. Originator: Setting state to State4 Originator: State after restoring from Memento: State3 {{Hidden end}}

{{Java/Hidden begin|title=Implementation in C#}}

Another way to implement memento in C#
Output: Originator(5,4)

}}