Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
"Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations." (Gamma, E., R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Reading, MA: Addison-Wesley, 1995)
Sometimes its necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request. For example, user interface toolkits include objects like buttons and menus that carry out a request in response to user input. But the toolkit cant implement the request explicitly in the button or menu, because only applications that use the toolkit know what should be done on which object. Toolkit designers have no way of knowing the receiver of the request or the operations that will carry it out. The Command pattern lets toolkit controls make requests of unspecified application objects by turning the request itself into an object. This object can be stored and passed around like other objects. Menus can be implemented easily with Command objects. Each choice in a Menu is an instance of a MenuItem class (Invoker in the thumbnail). An Application class (in the thumbnail) creates these menus and their menu items along with the rest of the user interface. The Application class also keeps track of Document objects (Receiver in the thumbnail) that a user has opened. The application configures each MenuItem with an instance of a concrete Command subclass. When the user selects a MenuItem, the MenuItem calls Execute on its command, and Execute carries out the operation. MenuItems dont know which subclass of Command they use. Command subclasses store the receiver of the request and invoke one or more operations on the receiver. For example, PasteCommand (ConcreteCommand in the thumbnail) supports pasting text from the clipboard into a Document. PasteCommands receiver is the Document object it is supplied upon instantiation. The Execute operation invokes Paste on the receiving Document
This pattern has been used on the following systems: The idea of commands for implementing undoable operations was popularized by MacApp. (Apple Computer, Inc., Cupertino, CA. Macintosh Programmers Workshop Pascal 3.0 Reference, 1989.) InterViews defines an abstract class, Action, that provides command functionality. To instantiate command subclasses automatically, InterViews also defines an ActionCallback template, parameterized by action method. (Linton, M., P. Calder, J. Interrante, S. Tang, J. Vlissides. InterViews Reference Manual. CSL, Stsandford University, 3.1 edition, 1992.) Unidraws command objects behave like messages. A command may be sent to another object for interpretation and that object may delegate that interpretation to another, usually this would be the receivers parent. The receiving object is computed not stored. The interpretation of the command varies with the receiving object. The interpretation mechanism depends on run-time type information.
Composite, Memento, Prototype, and Command patterns

Prototype pattern, Composite pattern, Command pattern, Memento pattern, behavioral patterns, Command Processor pattern, Gamma patterns, Action pattern, Transaction pattern, encapsulation (request), support undo, history list, callbacks, request manager
any system with sets of objects that can handle similar requests
need to modify object behaviors dynamically without affecting existing objectsneed to queue requests for future execution - such as undo
increases reuse of classes by de-coupling the interface from the implementationprovides weak coupling between clients and subsystemsallows you to vary the behavior of a class independent of its context
increased number of objects
Test driver Test.javaSimple implentation of the pattern - Receiver class. Receiver.javaSimple implentation of the pattern - ConcreteReceiver class. ConcreteReceiver.javaSimple implentation of the pattern - ConcreteCommand class. ConcreteCommand.javaSimple implentation of the pattern - Command class. Command.javaSimple implentation of the pattern - Client class. Client.javaTest driver testcommand.cppSimple implementation of the pattern. command.h