Memento pattern 00:00:00 Make a prediction You will define what undo restores and decide how to snapshot it without exposing an editor's internals. Undo restores all the draft's characters, but the cursor jumps to the beginning and the selection disappears. Has the editor restored the user's previous editing state? Only part of it. If the undo contract promises an editing state, the snapshot must include cursor and selection along with the text. The history manager need not interpret those fields. 00:00:32 Noor, Max and the plaster casts A child takes a plaster cast of a sand castle. A wave knocks the towers down. The friend who kept the sealed cast can hand it back without knowing how to recreate the towers; their builder knows how to use it. The originator captures and restores its own state. The caretaker holds snapshots without learning private representation details. Memento is a behavioral design pattern. An object, the originator, puts its state into a second object, the memento. A third object, the caretaker, keeps it and gives it back when the state is wanted again. Going back needs the old state, and the old state is private. Copying it from outside means that other classes must know all fields. They break whenever a field is added, and they can put the object into a state it would never enter by itself. 00:01:25 Restore a finished draft A draft contains text, cursor position and selection. Before a risky edit, ask the editor to create a snapshot of all state the user expects undo to restore. The history manager holds that opaque snapshot without reaching inside the editor. The editor remains responsible for encoding and restoring it. Undo the edit and compare text, cursor and selection. A screenshot of the text alone would miss two parts of the promise. Save the draft and cursor, delete the last character, then undo. Restoring only the word leaves the cursor at the wrong position. A memento should capture the state covered by the user's promise, even when two pieces of that state must change together. 00:02:11 Budget the cost of history A painting application can snapshot a large canvas after every stroke, but memory grows quickly. It might combine occasional full snapshots with smaller changes between them. Define restoration accuracy first, then choose the tradeoff between storage and replay time. One hundred full snapshots of a ten-megabyte document can approach a gigabyte before overhead. A checkpoint plus smaller changes can lower that cost but makes restore perform more work. Measure the restore time as well as memory before choosing the history representation. An undo snapshot with references to mutable live arrays can change after capture and cease to represent the past. Verify the restored values after subsequent edits. Use recorded commands when inverse operations are cheap and reliable; snapshots suit situations where reconstructing a prior state is simpler. Python’s random generator can capture its current internal state and restore that state later. Generate a value, restore the snapshot, and generate again using the same generator and algorithm. This makes a clean memento example: the caller can store the snapshot without needing to interpret its internal representation. 00:03:29 Try an interview follow-up How does undo change if the editor is extended by plug-ins with their own state? Each plug-in is an originator with the same small interface. The history takes a composite memento: one per plug-in, all or nothing, with the version of the plug-in. A plug-in that is missing on restore keeps its part closed and untouched, which works because caretakers never look inside. Budget per plug-in, and a fallback if one raises during restore. An editor snapshot restores text after undo, but the cursor still points beyond the end of the restored document. Capture the selection together with the text when both belong to the undo promise. Type, select, delete and undo. Verify the restored text and selection without giving the history manager access to internal fields. 00:04:24 What to take with you The originator makes a closed copy of its state, the caretaker keeps it, and only the originator opens it again. The copy must be its own, fixed and complete. Undo: save before each change, save the present before going back, restore before moving entries, and clear redo on a new change. The present is on no stack.