intermediateMemento Pattern

In the Memento pattern, why shouldn't the Caretaker be able to read the contents of a Memento it's holding?

The Caretaker's entire job is to manage when snapshots get taken and restored, not to know anything about what's inside them — that knowledge belongs solely to the Originator that created the snapshot. If the Caretaker could freely read a Memento's fields, the Originator's internal representation would leak out to a class that has no business understanding it, defeating the point of encapsulation. In Java this is often enforced by giving the Memento's data-access method package-private (default) visibility instead of public, so only classes in the same package — realistically just the Originator — can call it. Letting the Caretaker peek inside also creates a hidden coupling: if the Originator's internal fields ever change shape, any code that was reading them directly would break, even though that code was never supposed to depend on those details in the first place.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to How do you identify whether a class violates the Single Responsibility Principle?← Back to all Low-Level Design & Design Patterns questions