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
Commandobjects:-
Define a common interface
for performing a request
(
Command | execute()). -
Define classes (
Command1,…) that implement theCommandinterface. -
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()).
-
Define a common interface
for performing a request
(
-
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 aCommandobject (command.execute()). -
This enables run-time flexibility (via object
composition).
A class can be configured with aCommandobject, which it uses to perform a request, and even more, theCommandobject 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
Commandinterface to perform a request (command.execute())
and is independent of how the request is implemented. -
Maintains a reference
(
command) to aCommandobject.
-
Refers to the
-
Command- Defines a common interface for performing a request.
-
Command1,…-
Implement the
Commandinterface (for example, by callingaction1()on aReceiver1object). - See also Implementation.
-
Implement the
Dynamic Object Collaboration
-
In this sample scenario,
an
Invokerobject delegates performing a request to aCommandobject.
Let's assume thatInvokeris configured with aCommand1object. -
The interaction starts with the
Invokerobject that callsexecute()on its installedCommand1object. -
Command1callsaction1()on aReceiver1object. - 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.