Command 00:00:00 Make a prediction You will turn an action into a value that can be queued or undone and specify exactly which old state undo requires. A text editor replaced red with blue. The undo record contains only the new word blue. Can it reliably restore the original text? No. It must know the position and the prior content, or keep equivalent state elsewhere. Reconstructing yesterday's value from today's result is not generally possible. 00:00:33 The toy crane and its punched cards A toy crane reads cards for grab, move right three and drop. To undo, lift the last card first and reverse that particular action using what the crane recorded. Undoing the first card while still holding the block at its destination changes the story. A command packages an intention plus the context it needs. Undo order and saved prior state make replay meaningful, rather than a hopeful inversion. Command is a behavioral design pattern. A request becomes an object with one main method, execute, and often a second one, undo. It separates when from what. A button, a menu, a key, a timer and a network worker can trigger the same wish. 00:01:21 Undo a text edit correctly Replace the word red with blue at a known position. To undo reliably, the command records the original text and where it was before editing. Execute delegates the change to the document. Once it succeeds, put the command on the history stack. Undo restores red at that location. A second command changing another location must be undone first, or old positions may no longer refer to the same text. Delete the three letters starting at position five, then move the cursor elsewhere. Undo should put the letters back at position five because that is where the command actually executed. Remembering only the delete shortcut would make undo depend on whatever is selected now. 00:02:12 Queue a print request A print button creates a command with the document identifier and print options. A work queue can run it later without knowing the printer's details. Decide whether the command captures a document snapshot or reads the latest revision, because the two behaviours print different content. Queue a print job, then edit the document before the printer wakes. A snapshot contract prints revision three; a live-reference contract can print four. Either is implementable, but the choice determines exactly what a command must store alongside its execution request. Sending a replacement command to a real printer cannot unprint physical paper. Undo may require compensation or may simply be unavailable. Use a direct method call when the action never needs to outlive the call, be logged, replayed or composed. Qt undo framework. Qt calls its undo framework an implementation of the Command pattern. QUndoStack merges commands with mergeWith, has an undo limit, macros, and a clean state for the saved document. Swing Action. The Action interface is useful when the same functionality is reached from several controls: menu, toolbar and key share one object, with its text and its enabled state. 00:03:36 Try an interview follow-up How do undo and redo work? I keep one stack for completed commands and one for undone commands. Executing a fresh edit clears the redo stack. Undo moves the last completed command across after restoring its prior state. Redo moves it back after reapplying it. Without clearing redo after a new edit, an old command could replay against a different document. An editor records a delete command at position five. Later the selection moves. To undo the deletion, retain the deleted text and the position at execution time. Use those recorded facts to restore the edit. Reading the current selection during undo would target a different part of the document. 00:04:25 What to take with you A command holds receiver, action and parameters. The invoker runs and keeps commands, the receiver knows nothing about them. Slots hold recipes, so that each press has its own object and its own memory. Undo restores what execute overwrote. Two stacks, or one list with a position, and a new command drops the redo part. Macros undo backwards and take back finished parts when one fails.