Applied Design Patterns with Java
Behavioral :: Command (233) {C ch 17}
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 undoable, ConcreteCommand stores state for undoing the command prior to invoking Execute.
- The ConcreteCommand object invokes operations on its receiver to carry
out the request.
The following Interaction (Sequence) Diagram shows the
interactions between these objects. It illustrates how Command decouples the invoker from the receiver (and the request it carries
out):
Consequences
Command
has the following benefits and liabilities:
- Reduced coupling. Command decouples the object that invokes
the operation from the one that knows how to perform it. The use of Delegates makes this possible.
- Reusability.
Commands
are first-class objects. They can be manipulated and extended like any other object.
- You can assemble Commands into a composite
Command. An example is the MacroCommand class described
earlier. In general, composite Commands are an instance of the Composite
(163) pattern.
- Extensibility.
It's easy to add new Commands, as there is no need to change existing classes.
Catalog Behavioral Prev Next