A Memento Pattern says that "to restore the state of an object to its previous state". But it must do this without violating Encapsulation. Such case is useful in case of error or failure.
The Memento pattern is also known as Token.
Undo or backspace or ctrl+z is one of the most used operation in an editor. Memento design pattern is used to implement the undo operation. This is done by saving the current state of the object as it changes state.
UML for Memento Pattern:
Implementation of Memento Pattern:
Create an Originator class that will use Memento object to restore its previous state.
Create a Memento class that will Store internal state of the Originator object.
Create a Caretaker class that will responsible for keeping the Memento.
Create a MementoPatternDemo class.