Try a swap, keep it or put it back
A nurse-rota solver spends most of its night taking things back. It builds the next four weeks of shifts for a hospital ward, one small move at a time: swap two nurses' shifts, score the result, keep the swap if the score went down (lower is better: fewer broken rules, fairer hours), and otherwise put the rota back exactly as it was. Most moves are rejected, so putting things back is the operation it runs most.
The rota is a Rota object. Besides the table of assignments, 60 nurses by 28 days, it keeps derived data so that scoring a move stays fast: the hours each nurse works, and a count of broken rules, such as a day shift that starts less than eleven hours after the same nurse's night shift ended. rota.swap(a, b) updates all of it together.
The obvious place for the undo is the solver: copy rota.assignments before a move and assign the copy back after a rejected one. That restores the assignments and nothing else. The hours and the rule count still describe the rejected swap, so the next move is scored against numbers that belong to no rota. Copying those two fields as well fixes today's Rota, but now the solver has to know every field the class keeps, including any added later, such as a cache of each nurse's run of consecutive nights, in a class the solver's authors never edit.
Let the rota pack itself
The copying belongs in the one class that knows what its state is. rota.save() returns a RotaMemento holding everything a Rota needs to become this rota again: the assignments, the counters, the caches. rota.restore(m) puts all of it back. The solver's loop saves, swaps, scores, and calls restore(m) when the score did not go down. When the nights cache arrives, save() and restore() change beside it, in Rota, and the solver does not change at all.
That arrangement is the Gang of Four's Memento, and it has three roles. The originator is the object whose state is saved, here Rota: it creates the memento and is the only code meant to read it back. The memento is the saved state, packed into an object. The caretaker decides when to save and when to restore, and keeps the mementos in between without ever looking inside, here the solver.
The solver (caretaker) asks the rota (originator) to save, keeps the RotaMemento without opening it, and hands it back to restore. Only Rota reads what is inside.
Why insist that the solver cannot look? A caretaker that reads the memento's fields depends on how Rota is built inside, the same dependency the solver's own copying had. The Gang of Four describe a memento with two interfaces: a wide one the originator uses to write and read the saved state, and a narrow one, often empty, which is all the caretaker sees. How firmly that line holds depends on the language; in Java, for instance, the memento can be a static nested class of Rota with private fields, which Rota can read and other classes cannot.
Rota takes back a state it never produced, with hour counts that no longer add up from its assignments, and every score after the restore is computed from them.Restored, and still wrong
Writing save() has a trap of its own. In JavaScript, copying the assignments with slice() looks like enough. But each entry is an object, { nurse, shift }, and swap edits entries in place, a.nurse = b.nurse. The array in the memento is new, yet it points at the same entry objects the move then changes, so restore(m) puts back an array whose entries already carry the rejected swap.
That is a shallow copy: a new container holding references to the same inner objects. A memento needs its own copy of every piece of state that can change after the save, which means a deep copy of those parts. Parts that never change, such as the nurse records the entries point to, can be shared safely. Prototype faces the same question of what to copy and what to share, for clones. The other way out is to make the entries immutable, so that swap replaces an entry instead of editing it; a shallow copy of the array is then enough, because nothing it points to will change.
The price of copying everything
A complete save() copies all 1,680 assignments and every counter to undo a move that touched two. Over the hundreds of thousands of moves in a night's run, much of the solver's time can go into mementos thrown away a moment later. Memory is not the issue here, because the solver holds a single memento, the one for the move in progress. Memory becomes the issue when a caretaker keeps many: a memento per step of a long history costs the size of the whole state times the number of steps.
So mementos often hold less. One can hold only the part of the state a move is about to change, the two entries and the counters they affect, as long as Rota still creates it and reads it back. The caretaker then has to name the move before making it, as in rota.saveFor(a, b), but it still never looks inside. Mementos like that each know only the state just before their own move, so when several are kept they have to be restored newest first.
Beside a command, unlike a checkpoint
Undo in an app with a history is where Memento most often appears, next to a command: Command turns each action into an object, and a command whose action has no exact inverse can keep a memento of what it is about to change, so that its undo() restores it. Snapshot, despite the similar word, is a checkpoint saved beside an event log so that rebuilding state replays fewer events, where a memento lets one object put back an earlier state of its own without exposing its internals.
Drawing the solver in the Challenge
The Memento Pattern Challenge asks, in up to eight nodes, for the three roles, with a caretaker that never opens a memento, and the save and restore flows between originator and caretaker. The solver's loop fits it: a Component as the originator, a Memento it creates, a Coordinator as the caretaker, and two calls from the caretaker to the originator, one to save and one to restore when a move is rejected, so the undo can be read off the drawing. What the caretaker touches is the real test. It calls the originator and holds mementos; an edge where it reads or rewrites a memento's contents puts the solver back to knowing how Rota is built. The design works when the originator returns to an exact earlier state and the caretaker never learns what that state was.