The 23 GoF design patterns and Dependency Injection – choose a pattern Abstract Factory design patternBuilder design patternFactory Method design patternPrototype design patternSingleton design patternAdapter design patternBridge design patternComposite design patternDecorator design patternFacade design patternFlyweight design patternProxy design patternChain of Responsibility design patternCommand design patternInterpreter design patternIterator design patternMediator design patternMemento design patternObserver design patternState design patternStrategy design patternTemplate Method design patternVisitor design patternDependency Injection design pattern

Command Design Pattern

Behavioral pattern. Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.

The Command design pattern solves problems like: How can coupling the invoker of a request to a request be avoided?

Open in the interactive referenceDownload the PDF reference

Problem

The Command design pattern solves problems like:
How can coupling the invoker of a request to a request be avoided? How can an object be configured with a request?
See Applicability section for all problems Command can solve. See Solution section for how Command solves the problems.

  • An inflexible way is to implement (hard-wire) a request (receiver1.action1()) directly within the class (Invoker) that invokes the request.
  • This commits (couples) the the invoker of a request to a particular request at compile-time and makes it impossible to specify a request at run-time.
    "When you specify a particular operation, you commit to one way of satisfying a request. By avoiding hard-coded requests, you make it easier to change the way a request gets satisfied both at compile-time and run-time." [GoF, p24]
  • That's the kind of approach to avoid if we want to configure an object with a request at run-time.
  • For example, reusable classes that invoke a request in response to an user input.
    A reusable class should avoid hard-wired requests so that it can be configured with a request at run-time.

Solution

The Command design pattern provides a solution:
Encapsulate a request in a separate Command object.
A class delegates a request to a Command object
and doesn't know (is independent of) how the request performed.
Describing the Command design in more detail is the theme of the following sections.
See Applicability section for all problems Command can solve.

  • The key idea in this pattern is to encapsulate a request in a separate object that (1) can be used and passed around just like other objects and (2) can be queued or logged to be called at a later point. [BGI]
  • Define separate Command objects:
    • Define a common interface for performing a request (Command | execute()).
    • Define classes (Command1,…) that implement the Command interface.
    • A command can implement arbitrary functionality. In the most simple case, it implements a request by calling an operation on a receiver object (receiver1.action1()).
  • This enables compile-time flexibility (via inheritance).
    New commands can be added and existing ones can be changed independently by defining new (sub)classes.
  • A class (Invoker) delegates the responsibility for performing a request to a Command object (command.execute()).
  • This enables run-time flexibility (via object composition).
    A class can be configured with a Command object, which it uses to perform a request, and even more, the Command object can be exchanged dynamically.
    Commands can be stored (in a history list, for example) to be executed or unexecuted at a later time (to queue or log requests and to support undoable operations).

Background Information

  • In a procedural language, a callback function is "a function that's registered somewhere to be called at a later point. Commands are an object-oriented replacement for callbacks." [GoF, p235]

Applicability

Design Problems

  • Avoiding Hard-Wired Requests
    • How can coupling the invoker of a request to a request be avoided?
  • Exchanging Requests at Run-Time
    • How can an object be configured with a request?
    • How can a request be selected and exchanged at run-time?
  • Queuing or Logging Requests
    • How can requests be queued or logged?
    • How can undoable operations be supported?

Structure

Static Class Structure

  • Invoker
    • Refers to the Command interface to perform a request (command.execute())
      and is independent of how the request is implemented.
    • Maintains a reference (command) to a Command object.
  • Command
    • Defines a common interface for performing a request.
  • Command1,…
    • Implement the Command interface (for example, by calling action1() on a Receiver1 object).
    • See also Implementation.

Dynamic Object Collaboration

  • In this sample scenario, an Invoker object delegates performing a request to a Command object.
    Let's assume that Invoker is configured with a Command1 object.
  • The interaction starts with the Invoker object that calls execute() on its installed Command1 object.
  • Command1 calls action1() on a Receiver1 object.
  • See also Sample Code / Example 1.

Not included on this page: Intent, Motivation, Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.