Card 09/ 09
Recap: Command Pattern
A request as an object: queue it, log it, and undo it if it remembers what it changed.
Command turns a request into an object that can be held before, and after, it runs.
- Intent. Encapsulate a request as an object, to parameterise, queue, log or undo it.
- Participants. The command interface, the concrete command, the receiver that does the work, and the invoker that runs commands and may keep a history.
- Undo. A stack of commands; each saves in
executewhat its change destroys, and restores it inundo. - In the wild. Java's
RunnableandCallable, queued and run by anExecutorService. - In Python. A function or
functools.partialis a command that only runs; undo needs a class. - The trap. An undo that tries to read the old state from the receiver, which no longer has it.
- When not. A single immediate call that is never stored, queued or reversed.
← → move through sections and cards