Command

Intent

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Structure

Participants

Notes

One of the interesting aspects of the Command design pattern is that it serves several purposes. This results in higher-level design patterns - patterns where the Command design pattern is combined in predictable ways with other design patterns.

Commands as User Actions

When the Command design pattern is used to support user actions in a graphical user interface, some of the Command participants also are participants in the Observer design pattern.

Complex Commands

A complex command can be broken down into a sequence of simpler commands. The cleanest way of supporting this decomposition is to use the Composite design pattern.

Undoable Operations

A command may be undoable. This often involves the Memento design pattern.

Examples

The Java javax.swing and javax.swing.text packages contain several classes that jointly illustrate the Command design pattern.