Memento PatternA 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. Benefits:
Usage:
UML for Memento Pattern:Memento:
Originator:
Caretaker:
Implementation of Memento Pattern:Step 1: Create an Originator class that will use Memento object to restore its previous state. Step 2: Create a Memento class that will Store internal state of the Originator object. Step 3: Create a Caretaker class that will responsible for keeping the Memento. Step 4: Create a MementoPatternDemo class. Output: Next TopicObserver Design Pattern |