Bird
0
0

Why does the Memento pattern emphasize keeping the Memento's state private and inaccessible to other objects except the Originator?

hard🧠 Conceptual Q10 of 15
LLD - Behavioral Design Patterns — Part 2
Why does the Memento pattern emphasize keeping the Memento's state private and inaccessible to other objects except the Originator?
ATo preserve encapsulation and prevent external objects from modifying internal state
BTo allow any object to modify the saved state freely
CTo simplify the Memento class by removing getters and setters
DTo enable the Caretaker to change the Originator's state directly
Step-by-Step Solution
Solution:
  1. Step 1: Understand encapsulation principle

    Encapsulation means hiding internal details to protect object integrity.
  2. Step 2: Apply to Memento pattern

    Memento keeps state private so only Originator can restore it, preventing external tampering.
  3. Final Answer:

    To preserve encapsulation and prevent external objects from modifying internal state -> Option A
  4. Quick Check:

    Encapsulation protects internal state [OK]
Quick Trick: Keep Memento state private to protect it [OK]
Common Mistakes:
MISTAKES
  • Allowing external objects to modify Memento state
  • Thinking Caretaker can change state
  • Ignoring encapsulation importance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes