COMMAND
COMMAND
COMMAND
Object
Interaction
Diagram for
Command using
Diner Example
The Command pattern allows requests to be encapsulated as
objects, thereby allowing clients to be paramaterized with
different requests.
The "check" at a diner is an example of a Command pattern.
The waiter or waitress takes an order, or command from a
customer, and encapsulates that order by writing it on the
check. The order is then queued for a short order cook. Note
that the pad of "checks" used by different diners is not
dependent on the menu, and therefore they can support
commands to cook many different items.
Behavioral Command
Intent
Encapsulate a request as an object, thereby letting you
parameterize clients with different requests, queue or
log requests, and support undoable operations.
Also Known As
Action, Transaction
Problem:
Need to issue requests to objects without knowing anything
about the operation being requested or the receiver of the
request.
Behavioral Command
Solution
• Command decouples the object that invokes the operation
from the one that knows how to perform it. To achieve this
separation, the designer creates an abstract base class that
maps a receiver (an object) with an action (a pointer to a
member function). The base class contains an execute()
method that simply calls the action on the receiver.
• All clients of Command objects treat each object as a "black
box" by simply invoking the object's virtual execute()
method whenever the client requires the object's "service".
• Sequences of Command objects can be assembled into
composite (or macro) commands.
Behavioral Command
Applicability
Use the Command pattern when you want to
parameterize objects by an action to perform, as MenuItem.
You can express such parameterization in a procedural
language with a callback function, that is, a function that’s
registered somewhere to be called at a later point.
Commands are an object Oriented replacement for
callbacks.
specify, queue, and execute requests at different times. A
Command object can have a lifetime independent of the
original request. If the receiver of a request can be
represented in an address space-independent way, then you
can transfer a command object for the request to a different
process and fulfill the request there.
Behavioral Command
Applicability……….
Use the Command pattern when you want to
Supports Undo
Support logging changes to that they can be
reapplied in case of a system crash. - Recovering
form a crash involves reloading logged commands
form disk and re-executing them with the execute
operation.
Structure a system around high-level operations built
on primitives operations. Such structure is common
in information system that support transactions.
Behavioral Command
Structure
Behavioral Command
Collaborations
The client creates a ConcreteCommand object and
specifies its receiver.
An Invoker object stores the ConcreteCommand
object.
The invoker issues a request by calling Execute on
the command. When commands are undo-able,
ConcreteCommand stores state for undoing the com-
mand prior to invoking Execute.
The ConcreteCommand object invokes operations
on its receiver to carry out the request.
Behavioral Command
Collaborations
Consequences
The Command pattern has the following consequences: