Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the main purpose of the Memento pattern?
The Memento pattern helps save and restore an object's state without exposing its internal details. It allows undo or rollback operations safely.
Click to reveal answer
beginner
Which three roles are involved in the Memento pattern?
1. Originator: The object whose state is saved. 2. Memento: Stores the saved state. 3. Caretaker: Manages mementos but does not modify them.
Click to reveal answer
intermediate
Why should the Memento hide its state from other objects except the Originator?
To protect encapsulation and prevent other objects from changing the saved state directly, ensuring data integrity.
Click to reveal answer
intermediate
How does the Caretaker interact with the Memento in the pattern?
The Caretaker requests a Memento from the Originator to save state and later gives the Memento back to restore state, but it never changes the Memento's content.
Click to reveal answer
beginner
Give a real-life example that illustrates the Memento pattern.
Think of a text editor with an undo feature. The editor saves snapshots of the document (Mementos). When you undo, it restores the document to a previous snapshot without exposing how the document stores data internally.
Click to reveal answer
Which component in the Memento pattern is responsible for creating a snapshot of the object's state?
AOriginator
BCaretaker
CMemento
DClient
✗ Incorrect
The Originator creates a Memento that stores its current state.
What is the role of the Caretaker in the Memento pattern?
AStore and manage Mementos without changing them
BModify the saved state
CCreate Mementos
DRestore the Originator's state directly
✗ Incorrect
The Caretaker keeps Mementos safe but does not modify their content.
Why is it important that the Memento pattern preserves encapsulation?
ATo allow other objects to change the state
BTo avoid saving the state
CTo hide the internal state from all except the Originator
DTo make the state public
✗ Incorrect
Encapsulation ensures only the Originator can access and restore its state.
Which scenario best fits the use of the Memento pattern?
ALogging user actions
BUndo functionality in an application
CSharing data between objects
DEncrypting data
✗ Incorrect
Undo requires saving and restoring previous states, which Memento supports.
In the Memento pattern, who is responsible for restoring the saved state?
AMemento
BClient
CCaretaker
DOriginator
✗ Incorrect
The Originator uses the Memento to restore its own state.
Explain the roles and responsibilities of the Originator, Memento, and Caretaker in the Memento pattern.
Think about who saves, who holds, and who restores the state.
You got /3 concepts.
Describe a real-world example where the Memento pattern can be applied and explain why it fits.
Consider common software features that let you go back to a previous state.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the Memento pattern in system design?
easy
A. To create multiple instances of an object efficiently
B. To convert one interface to another compatible interface
C. To manage concurrent access to shared resources
D. To save and restore an object's state without exposing its internal details
Solution
Step 1: Understand the role of Memento pattern
The Memento pattern is designed to capture and externalize an object's internal state so that it can be restored later without exposing the object's implementation details.
Step 2: Compare with other design patterns
Other options describe different patterns: A is about object creation (Factory), C is about synchronization (Mutex), D is about interface compatibility (Adapter).
Final Answer:
To save and restore an object's state without exposing its internal details -> Option D
Quick Check:
Memento = Save & Restore State [OK]
Hint: Memento = save state secretly, no details shown [OK]
Common Mistakes:
Confusing Memento with Factory or Adapter patterns
Thinking it manages concurrency
Assuming it changes object interfaces
2. Which of the following correctly represents the key components of the Memento pattern?
easy
A. Subject, Observer, ConcreteObserver
B. Originator, Memento, Caretaker
C. Client, Proxy, RealSubject
D. Component, Decorator, ConcreteComponent
Solution
Step 1: Identify components of Memento pattern
The Memento pattern consists of three main parts: Originator (the object whose state is saved), Memento (the object storing the state), and Caretaker (manages mementos).
Step 2: Eliminate other patterns
Options B, C, and D correspond to Observer, Proxy, and Decorator patterns respectively, which are unrelated to Memento.
A. The save method returns state directly, exposing internal details
B. The restore method does not update the state
C. The Originator class lacks a Memento class
D. The set_state method is missing
Solution
Step 1: Analyze the save method
The save method returns the internal state directly instead of encapsulating it in a Memento object, exposing internal details.
Step 2: Understand Memento pattern principle
The pattern requires hiding the internal state inside a Memento object to prevent external access. Returning raw state breaks encapsulation.
Final Answer:
The save method returns state directly, exposing internal details -> Option A
Quick Check:
Save must hide state in Memento [OK]
Hint: Save must return Memento, not raw state [OK]
Common Mistakes:
Thinking restore method is faulty
Believing Memento class is mandatory in code
Ignoring encapsulation principle
5. You are designing a text editor with undo functionality using the Memento pattern. Which approach best balances memory usage and undo capability?
hard
A. Store a Memento only after significant changes or at checkpoints
B. Store a Memento after every single character change
C. Store all changes as raw text snapshots without Memento objects
D. Do not store any state; rely on user to retype
Solution
Step 1: Consider memory and undo tradeoff
Storing a Memento after every character change (Store a Memento after every single character change) uses excessive memory and is inefficient.
Step 2: Evaluate checkpoint strategy
Storing Mementos after significant changes or checkpoints (Store a Memento only after significant changes or at checkpoints) reduces memory use while allowing meaningful undo steps.
Step 3: Assess other options
Store all changes as raw text snapshots without Memento objects wastes memory by storing raw snapshots without encapsulation; Do not store any state; rely on user to retype removes undo capability.
Final Answer:
Store a Memento only after significant changes or at checkpoints -> Option A
Quick Check:
Checkpoint Mementos balance memory and undo [OK]
Hint: Save states at checkpoints, not every keystroke [OK]