The pattern is useful when an object needs to support operations such as undo, rollback, or checkpoint recovery. Instead of exposing the object's internal fields, its state is stored inside a separate object called a Memento.
The object whose state is being saved is known as the Originator, while another object called the Caretaker is responsible for storing and restoring mementos.
The Memento Pattern is commonly used in text editors, drawing applications, games, and applications that require version history or rollback functionality.
Structure
The Memento Pattern consists of the following participants.1. The Originator creates mementos containing its current state and restores its state from a memento.
2. The Memento stores the state of the originator.
3. The Caretaker maintains one or more mementos without accessing or modifying their contents.

Java Implementation
Suppose an application allows users to edit a document. Before every modification, the current content is saved so that the user can undo the last change if required.Create the originator.
public class Document {
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) {
this.content = memento.getContent();
}
}
Create the memento.
public class Memento {
private final String content;
public Memento(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
Create the caretaker.
public class History {
private final Stack<Memento> history = new Stack<>();
public void save(Memento memento) {
history.push(memento);
}
public Memento undo() {
return history.pop();
}
}
The client saves and restores document states.
public class Main {
public static void main(String[] args) {
Document document = new Document();
History history = new History();
document.setContent("Version 1");
history.save(document.save());
document.setContent("Version 2");
history.save(document.save());
document.setContent("Version 3");
document.restore(history.undo());
System.out.println(document.getContent());
}
}
Output:
Version 2
How It Works?
The Document object represents the originator whose state needs to be preserved.Whenever the document reaches a state that may need to be restored later, it creates a new Memento containing its current content.
The History object acts as the caretaker by storing each memento. It does not inspect or modify the stored state; it simply manages the collection of snapshots.
When an undo operation is requested, the caretaker returns the most recently saved memento to the document. The document then restores its state using the values stored inside that memento.
This approach keeps the document's internal state encapsulated while providing a simple mechanism for undo and rollback operations.
Advantages
1. It provides a simple mechanism for implementing undo and rollback operations.2. It preserves encapsulation by keeping the object's internal state hidden.
3. The caretaker manages state history without depending on the originator's implementation.
4. Multiple snapshots can be maintained to support version history.
Disadvantages
1. Saving many snapshots can increase memory consumption.2. Large objects may make snapshot creation expensive.
3. Managing long histories may require additional cleanup strategies.
JDK Examples
The Memento Pattern does not have many direct implementations in the Java standard library, but the concept is widely used.1. The javax.swing.undo.UndoManager maintains a history of edits, allowing applications to perform undo and redo operations.
2. Many IDEs, text editors, and document editing frameworks implement the Memento Pattern internally to maintain document history and restore previous states.
Summary
The Memento Pattern captures an object's state and stores it externally so that it can be restored later without exposing the object's internal implementation.It is widely used to implement undo, rollback, and version history features while preserving encapsulation and keeping state management separate from business logic.