How do you store a change history using Memento?
A change history is stored with Memento by creating a sequence of snapshots of an object's state (the Originator) and placing them into a data structure, usually a stack or a list. Each snapshot captures the object's state at a specific moment, and when needed you can roll back by restoring one of the past states.
1. Overall Structure
Three roles:
- Originator - creates a snapshot and restores itself from one;
- Memento - stores the state (usually immutable);
- Caretaker - manages the history (saves, returns snapshots).
2. Example Implementation (Java)
Memento (a state snapshot)
java
class Memento {
private final String state;
public Memento(String state) { this.state = state; }
public String getState() { return state; }
}Originator (the object whose state is saved)
java
class Editor {
private String text;
public void setText(String text) { this.text = text; }
public String getText() { return text; }
// Creates a snapshot
public Memento save() { return new Memento(text); }
// Restores state from a snapshot
public void restore(Memento memento) { text = memento.getState(); }
}Caretaker (manages the snapshot history)
java
class History {
private Stack<Memento> history = new Stack<>();
public void saveState(Memento memento) {
history.push(memento);
}
public Memento undo() {
if (!history.isEmpty())
return history.pop();
return null;
}
}Usage
java
Editor editor = new Editor();
History history = new History();
editor.setText("Version 1");
history.saveState(editor.save());
editor.setText("Version 2");
history.saveState(editor.save());
editor.setText("Version 3");
System.out.println(editor.getText()); // → Version 3
editor.restore(history.undo());
System.out.println(editor.getText()); // → Version 2
editor.restore(history.undo());
System.out.println(editor.getText()); // → Version 13. How It Works
- After every change, the object creates a snapshot (via
save()), and the Caretaker pushes it onto the stack. - When a rollback is needed, the Caretaker returns the latest snapshot,
and the Originator restores its state (
restore(memento)). - This creates a change history, an analog of undo/redo.
4. Extension: Redo and Limiting the History
- For redo, you can keep a second stack that holds undone snapshots.
- To avoid running out of memory, you can limit the length of the history, discarding old entries.
5. Advantages
- The history is stored without breaking the object's encapsulation.
- You can "rewind" the state to any step back.
- A simple and safe implementation of undo/redo.
Conclusion
Storing history with Memento comes down to three steps:
- After every change, create a snapshot of the state.
- Save the snapshot to the stack (Caretaker).
- On rollback, restore the state from the latest snapshot.
Summary:
Memento turns a change history into a sequence of "time snapshots", letting you safely roll back without breaking the object's encapsulation.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.