27. Memento
Handing out state that only its author can read, so undo costs the holder no access
This one is for reading. Every file is locked, and Run is the only button.
Three roles, and the interesting one is the one that does least. The Editor is the originator: it makes a Memento and it is the only thing that can make sense of one. The Memento is the saved state, with no accessor a stranger could reach. History is the caretaker: it holds mementos in order and gives them back, and that is the whole of its job.
Look at what History never does. It never reads the text, never compares two saves, never decides whether one is worth keeping. It could not if it wanted to — there is nothing on a Memento to call. Undo works, and the class that stores the undo history has no idea what is in it.
The reason this is a demo is the lesson itself. Every assertion in Tests.java goes through the Editor — the text, the caret, the restore — because there is no other way in. To check a Memento directly a test would need an accessor on it, and adding that accessor would undo the exact property the test was written to check. The pattern’s guarantee is an absence, and you cannot assert an absence by reaching for it.
So there is no change request to drop on this. What a change request grades is whether new behaviour lands without editing your code, and the thing worth grading here is that a second class cannot see something — which is a fact about the code as written, not about how it bends. Read it instead, and notice how little History is allowed to know.
Run runs Tests.java exactly as it is written. There is nothing to edit and nothing to submit.
- Ctrl/⌘ + Enter runs the tests
Run it and watch it pass.