RecapDifficulty: Advanced1 min

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 execute what its change destroys, and restores it in undo.
  • In the wild. Java's Runnable and Callable, queued and run by an ExecutorService.
  • In Python. A function or functools.partial is 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.