YouTip LogoYouTip

Memento Pattern

# Memento Pattern ## Memento Pattern The Memento Pattern saves the state of an object so that it can be restored at an appropriate time. It is a behavioral design pattern. The Memento Pattern allows capturing and restoring an object's internal state without violating encapsulation. ### Intent Capture an object's internal state without breaking encapsulation, and allow saving and restoring these states outside the object. ### Main Problem Solved * Allows capturing and saving an object's internal state so that it can be restored later, enabling undo and rollback operations. ### Use Cases * When an undo mechanism is needed, allowing users to revert to a previous state. ### Implementation Approach * **Create a Memento class**: Used to store and encapsulate the object's state. * **Create an Originator role**: Responsible for creating the Memento and restoring state as needed. * **Create a Memento Manager class** (optional): Responsible for managing all Memento objects. ### Key Code * **Memento**: Stores the state information of the Originator. * **Originator**: Creates the Memento and restores state based on it. ### Application Examples * **"Regret Medicine"**: Provides an undo operation function. * **Game Save**: Saves game progress, allowing players to load previous saves. * **Ctrl+Z in Windows**: Implements undo operations. * **Back button in IE Browser**: Allows users to return to the previous page. * **Database Transaction Management**: Saves state via transaction logs to enable rollback. ### Advantages * **Provides a state recovery mechanism**: Allows users to easily return to historical states. * **Encapsulates state information**: Users do not need to worry about the details of state saving. ### Disadvantages * **Resource consumption**: If the object's state is complex, saving the state may consume significant resources. ### Usage Recommendations * Use the Memento Pattern in scenarios where data state needs to be saved and restored. * Consider using the Prototype Pattern in conjunction with the Memento Pattern to save memory. ### Notes * To reduce coupling, Memento objects should be managed indirectly through a Memento Manager class. * Use the Memento Pattern cautiously to avoid excessive consumption of system resources. ### Structure The Memento Pattern includes the following main roles: * **Memento**: Responsible for storing the internal state of the Originator. The Memento can hold part or all of the Originator's state information. * **Originator**: Creates a Memento object and can use the Memento object to restore its internal state. The Originator typically creates a Memento object when it needs to save its state and uses the Memento object when it needs to restore its state. * **Caretaker**: Responsible for keeping the Memento objects but does not operate on or inspect them. The Caretaker can only pass the Memento to other objects. The Memento Pattern uses three classes: _Memento_, _Originator_, and _Caretaker_. The Memento contains the state of the object to be restored. The Originator creates and stores the state in the Memento object. The Caretaker object is responsible for restoring the object's state from the Memento. _MementoPatternDemo_, our demo class uses _Caretaker_ and _Originator_ objects to demonstrate the restoration of an object's state. ![Image 1: UML diagram of the Memento Pattern](#) ### Step 1 Create the Memento class. ## Memento.java public class Memento{private String state; public Memento(String state){this.state = state; }public String getState(){return state; }} ### Step 2 Create the Originator class. ## Originator.java public class Originator{private String state; public void setState(String state){this.state = state; }public String getState(){return state; }public Memento saveStateToMemento(){return new Memento(state); }public void getStateFromMemento(Memento Memento){state = Memento.getState(); }} ### Step 3 Create the CareTaker class. ## CareTaker.java import java.util.ArrayList; import java.util.List; public class CareTaker{private ListmementoList = new ArrayList(); public void add(Memento state){mementoList.add(state); }public Memento get(int index){return mementoList.get(index); }} ### Step 4 Use the _CareTaker_ and _Originator_ objects. ## MementoPatternDemo.java public class MementoPatternDemo{public static void main(String[]args){Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); originator.setState("State #1"); originator.setState("State #2"); careTaker.add(originator.saveStateToMemento()); originator.setState("State #3"); careTaker.add(originator.saveStateToMemento()); originator.setState("State #4"); System.out.println("Current State: " + originator.getState()); originator.getStateFromMemento(careTaker.get(0)); System.out.println("First saved State: " + originator.getState()); originator.getStateFromMemento(careTaker.get(1)); System.out.println("Second saved State: " + originator.getState()); }} ### Step 5 Verify the output. Current State: State #4First saved State: State #2Second saved State: State #3
← Observer PatternMediator Pattern β†’