Memento Pattern

Memento Pattern with Patterns, design, Creational Design, Abstract Factory Pattern, singleton design patterns, Adapter, Visitor Pattern, Template Pattern, Command Pattern, State Pattern, java etc.

Memento Pattern

Memento Pattern

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.


Benefits:

  • It preserves encapsulation boundaries.
  • It simplifies the originator.

Usage:

  • It is used in Undo and Redo operations in most software.
  • It is also used in database transactions.

UML for Memento Pattern:

Memento:

  • Stores internal state of the originator object. The state can include any number of state variables.
  • The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus maintains the encapsulation. The other interface is Originator and it allows the Originator to access any state variables necessary to the originator to restore the previous state.

Originator:

  • Creates a memento object that will capture the internal state of Originator.
  • Use the memento object to restore its previous state.

Caretaker:

  • Responsible for keeping the memento.
  • The memento is transparent to the caretaker, and the caretaker must not operate on it.

Implementation of Memento Pattern:

Step 1:

Create an Originator class that will use Memento object to restore its previous state.

  1. //This is a class.  
  2.   
  3. public class Originator {  
  4.       
  5.        private String state;  
  6.       
  7.        public void setState(String state){  
  8.           this.state = state;  
  9.        }  
  10.       
  11.        public String getState(){  
  12.           return state;  
  13.        }  
  14.       
  15.        public Memento saveStateToMemento(){  
  16.           return new Memento(state);  
  17.        }  
  18.       
  19.        public void getStateFromMemento(Memento Memento){  
  20.           state = Memento.getState();  
  21.        }  
  22. }// End of the Originator class.  

Step 2:

Create a Memento class that will Store internal state of the Originator object.

  1. //This is a class.  
  2.   
  3. public class Memento {  
  4.       
  5.     private String state;  
  6.   
  7.     public Memento(String state) {  
  8.         this.state=state;  
  9.     }  
  10.     public String getState() {  
  11.         return state;  
  12.     }  
  13.       
  14. }// End of the Memento class.  

Step 3:

Create a Caretaker class that will responsible for keeping the Memento.

  1. //This is a class.  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6.   
  7. public class Caretaker {  
  8.       
  9.     private List mementoList = new ArrayList();  
  10.   
  11.        public void add(Memento state){  
  12.           mementoList.add(state);  
  13.        }  
  14.   
  15.        public Memento get(int index){  
  16.           return mementoList.get(index);  
  17.        }  
  18.   
  19. }// End of the Caretaker class.  

Step 4:

Create a MementoPatternDemo class.

  1. //This is a class.  
  2.   
  3.   
  4. public class MementoPatternDemo {  
  5.       
  6.     public static void main(String[] args) {  
  7.           
  8.           Originator originator = new Originator();  
  9.             
  10.           Caretaker careTaker = new Caretaker();  
  11.             
  12.           originator.setState("State #1");  
  13.           careTaker.add(originator.saveStateToMemento());  
  14.           originator.setState("State #2");  
  15.           careTaker.add(originator.saveStateToMemento());  
  16.           originator.setState("State #3");  
  17.           careTaker.add(originator.saveStateToMemento());  
  18.           originator.setState("State #4");  
  19.   
  20.           System.out.println("Current State: " + originator.getState());          
  21.           originator.getStateFromMemento(careTaker.get(0));  
  22.           System.out.println("First saved State: " + originator.getState());  
  23.           originator.getStateFromMemento(careTaker.get(1));  
  24.           System.out.println("Second saved State: " + originator.getState());  
  25.           originator.getStateFromMemento(careTaker.get(2));  
  26.           System.out.println("Third saved State: " + originator.getState());  
  27.        }  
  28.   
  29. }  
  30. // End of the MementoPatternDemo class.