Applied Design Patterns with Java
Behavioral :: Command (233) {C ch 17}
Applicability
Use the Command pattern when there is a need to:
- parameterize objects by an action to perform, as MenuItem
objects did above. Express such parameterization in a procedural language with a callback function, a function
that's registered somewhere to be called asynchronously 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, transfer a Command object for the request to a different process and fulfill the
request there;
- support 'undo'. The Command's Execute operation
can store state for reversing its effects in the command itself. The
Command interface must have an added Unexecute operation that reverses the effects of a previous call to Execute. Executed commands are stored in a history list. Unlimited-level
undo and redo is achieved by traversing this list backwards and forwards calling Unexecute and Execute, respectively;
- support logging changes so that they can be reapplied
in case of a system crash. Augmenting the Command interface with load and store operations allows keeping a persistent
log of changes. Recovering from a crash involves reloading logged commands from disk and reexecuting them with
the Execute operation;
- structure a system around high-level operations built
on primitives operations. Such a structure is common in information systems that support transactions.
A transaction encapsulates a set of changes to data.
The Command pattern offers a way to model
transactions. Commands have a common
interface to allow invoking all transactions the same way. The pattern also makes it easy to extend the system
with new transactions.
Structure
Participants
- ConcreteCommand (PasteCommand, OpenCommand) defines a binding between a Receiver object and an action,
and implements Execute by invoking the corresponding operation(s) on Receiver.
- Client (Application)
creates a ConcreteCommand object and sets its receiver.
- Invoker (MenuItem)
asks the command to carry out the request.
- Receiver (Document,
Application) knows how to perform the operations associated with carrying out a request. Any class may serve as
a Receiver.
Catalog Behavioral Prev Next