Design pattern "Memento"

Let's learn what is the "Memento" design pattern πŸ“ΈοΈπŸ–ΌοΈπŸ–ΌοΈπŸ”„πŸ•°οΈπŸ”
Monday, July 22, 2024

Real world example

The Memento Design Pattern is a behavioral design pattern that provides the ability to restore an object to its previous state. This pattern is used when we need to provide an undo mechanism in our application.

In the context of a text editor, here’s how it could work:

  1. The text editor (the Originator) creates a snapshot (the Memento) of its current state (the text) whenever a change is made.
  2. The history (the Caretaker) keeps track of all snapshots.
  3. When the user wants to undo a change, the text editor restores its state from the most recent snapshot in the history.
  4. When the user wants to redo a change, the text editor moves forward in the history and restores its state from the next snapshot.

In plain words

Memento pattern is about capturing and storing the current state of an object in a manner that it can be restored later on in a smooth manner.

Wikipedia definition

The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).

Programmatic example

import java.util.Stack;
// Originator
class TextEditor {
private String content;
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public Memento save() {
return new Memento(content);
}
public void restore(Memento memento) {
content = memento.getState();
}
}
// Memento
class Memento {
private final String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
}
// Caretaker
class UndoManager {
private final Stack<Memento> history = new Stack<Memento>();
public void saveState(TextEditor editor) {
history.push(editor.save());
}
public void undo(TextEditor editor) {
if (!history.isEmpty()) {
Memento memento = history.pop();
editor.restore(memento);
}
}
}
public class Main {
public static void main(String[] args) {
TextEditor editor = new TextEditor();
UndoManager undoManager = new UndoManager();
// Initial content
editor.setContent("Hello, World!");
System.out.println("Initial content: " + editor.getContent());
// Save state
undoManager.saveState(editor);
// Modify content
editor.setContent("Hello, Alex!");
System.out.println("Modified content: " + editor.getContent());
// Undo
undoManager.undo(editor);
System.out.println("After undo: " + editor.getContent());
}
}

Diagram

Loading graph...

Some other examples

Here are some real-world use cases:

  • Undo/Redo functionality: The Memento pattern is commonly used to implement undo/redo operations in applications such as text editors, graphics editors, or collaborative document editing tools.
  • Checkpointing and recovery: In systems where the state of an object needs to be periodically saved and restored, the Memento pattern can be used.
  • Game state management: Games often use the Memento pattern to save and restore game states during gameplay or between sessions.
  • Configuration management: When an application’s configuration settings change dynamically, the Memento pattern can help save and restore previous configurations.
  • Database transactions: Memento can be applied to manage database transactions, allowing rollback to a previous state in case of errors.
  • Browser history: Web browsers use Memento to store and navigate through visited pages, enabling users to go back and forth.
  • Collaborative editing tools: Memento helps maintain consistent states across multiple users in collaborative environments.
  • Version control systems: Memento-like mechanisms are used to track changes and revert to previous versions in source code repositories.
  • Simulation software: Memento can save and restore simulation states during complex simulations.
  • Drawing and graphic applications: Memento allows artists to undo/redo strokes and restore canvas states.

Remember, the Memento pattern provides flexibility and robustness by allowing objects to travel back in time! πŸ•°οΈπŸ”


Recommended articles