# Computer Science Design Patterns/Command

The command pattern is an object behavioural pattern that decouples sender and receiver. It can also be thought as an object-oriented equivalent of a call back method. Call back: It is a function that is registered to be called at a later point of time based on user actions.

## Scope

Object

## Purpose

Behavioral

## Intent

Encapsulate the request for a service as an object.

## Applicability

* to parameterize objects with an action to perform
* to specify, queue, and execute requests at different times
* for a history of requests
* for multilevel undo/redo

## Consequences

* + abstracts executor of a service
* + supports arbitrary-level undo-redo
* + composition yields macro-commands
* - might result in lots of trivial command subclasses

## Examples

The best example for this pattern is the graphical user interface. On most multidata interfaces, you have both a "New" menu and a "New" button with an icon like in Libre Office Writer. Both controls are connected to a command object, so the action is performed by the same code.

## Cost

This pattern is dealing with the whole architecture of a program. It may have a substantial impact on the project.

### Creation

If the code already exists, this pattern is very expensive.

### Maintenance

This pattern is very expensive to maintain.

### Removal

This pattern is very expensive to remove, too.

## Advises

* Use the Command term to indicate the use of the pattern to the other developers.

## Implementations

Consider a "simple" switch. In this example we configure the Switch with two commands: to turn the light on and to turn the light off. A benefit of this particular implementation of the command pattern is that the switch can be used with any device, not just a light — the Switch in the following example turns a light on and off, but the Switch's constructor is able to accept any subclasses of Command for its two parameters. For example, you could configure the Switch to start an engine.

## Operations

The implementations to do are:
* copying a command before putting it on a history list
* handling hysteresis
* supporting transactions
In the above example, it can be noted that the command pattern decouples the object that invokes the operation from the ones having the knowledge to perform it.

### Command in Java

Menus and buttons provide a classic example of the need for the command pattern. When you add a menu to an application, you have to configure the menu with words that describe actions that the user can choose, such as Save and Open. Similarly for a button. You also have to configure the menu or button so that it can take action, calling a method in response to a user's click. However, JMenuItem or JButton class has no way of knowing what action to perform when an item or button is selected. In the following example we use the same Action for both a menu item and a button saving us from having to write the same code twice.
Java Swing applications commonly apply the Mediator pattern, registering a single object to receive all GUI events. This object mediates the interaction of the components and translates user input into commands for business domain objects.

### Undo in Java

Swing provides for Undo/Redo functionality.
Consider a "simple" switch. In this example we configure the Switch with two commands: to turn the light on and to turn the light off.
A benefit of this particular implementation of the command pattern is that the switch can be used with any device, not just a light. The Switch in the following C# implementation turns a light on and off, but the Switch's constructor is able to accept any subclasses of Command for its two parameters. For example, you could configure the Switch to start an engine.
The following code is another implementation of command pattern in C#.
The following code is an implementation of command pattern in Python.
The following code is an implementation of command pattern in Javascript.
The following code is an implementation of command pattern in PHP.