EMZETT.
Login

Command pattern

In short: The command pattern packages an action as an object. That way you can store, delay, repeat or undo it.

In more detail: A command object contains everything needed to run it (action, receiver, parameters) and a method execute(), often also undo(). A caller (invoker) executes commands without knowing their details.

In Depth

  • Undo/redo: A stack of executed commands enables undo and redo, as in editors.
  • Queues: Commands can be put in a queue and processed later (message queue).
  • Macros: combine several commands into one.
  • Examples: menu and keyboard commands, transactions, task schedulers, event sourcing.
  • Related: strategy pattern chooses a method, command encapsulates a request.

See also: Strategy pattern, Observer pattern