Browser docs

Command

Also known as

Action, Transaction

Intent

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

Explanation

Real-world example

There is a wizard casting spells on a goblin. The spells are executed on the goblin one by one. The first spell shrinks the goblin and the second makes him invisible. Then the wizard reverses the spells one by one. Each spell here is a command object that can be undone.

In plain words

Storing requests as command objects allows performing an action or undoing it at a later time.

Wikipedia says

In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time.

Programmatic Example

Here’s the sample code with wizard and goblin. Let’s start from the Wizard class.

 1@Slf4j
 2public class Wizard {
 3
 4  private final Deque<Command> undoStack = new LinkedList<>();
 5  private final Deque<Command> redoStack = new LinkedList<>();
 6
 7  public Wizard() {}
 8
 9  public void castSpell(Runnable runnable) {
10    runnable.run();
11    undoStack.offerLast(runnable);
12  }
13
14  public void undoLastSpell() {
15    if (!undoStack.isEmpty()) {
16      var previousSpell = undoStack.pollLast();
17      redoStack.offerLast(previousSpell);
18      previousSpell.run();
19    }
20  }
21
22  public void redoLastSpell() {
23    if (!redoStack.isEmpty()) {
24      var previousSpell = redoStack.pollLast();
25      undoStack.offerLast(previousSpell);
26      previousSpell.run();
27    }
28  }
29
30  @Override
31  public String toString() {
32    return "Wizard";
33  }
34}

Next, we have the goblin who’s the target of the spells.

 1@Slf4j
 2public abstract class Target {
 3
 4  private Size size;
 5
 6  private Visibility visibility;
 7
 8  public Size getSize() {
 9    return size;
10  }
11
12  public void setSize(Size size) {
13    this.size = size;
14  }
15
16  public Visibility getVisibility() {
17    return visibility;
18  }
19
20  public void setVisibility(Visibility visibility) {
21    this.visibility = visibility;
22  }
23
24  @Override
25  public abstract String toString();
26
27  public void printStatus() {
28    LOGGER.info("{}, [size={}] [visibility={}]", this, getSize(), getVisibility());
29  }
30}
31
32public class Goblin extends Target {
33
34  public Goblin() {
35    setSize(Size.NORMAL);
36    setVisibility(Visibility.VISIBLE);
37  }
38
39  @Override
40  public String toString() {
41    return "Goblin";
42  }
43
44  public void changeSize() {
45    var oldSize = getSize() == Size.NORMAL ? Size.SMALL : Size.NORMAL;
46    setSize(oldSize);
47  }
48
49  public void changeVisibility() {
50    var visible = getVisibility() == Visibility.INVISIBLE
51          ? Visibility.VISIBLE : Visibility.INVISIBLE;
52    setVisibility(visible);
53  }
54}

Finally, we have the wizard in the main function casting spells.

 1public static void main(String[] args) {
 2  var wizard = new Wizard();
 3  var goblin = new Goblin();
 4
 5  // casts shrink/unshrink spell
 6  wizard.castSpell(goblin::changeSize);
 7
 8  // casts visible/invisible spell
 9  wizard.castSpell(goblin::changeVisibility);
10
11  // undo and redo casts
12   wizard.undoLastSpell();
13   wizard.redoLastSpell();

Here’s the whole example in action.

 1var wizard = new Wizard();
 2var goblin = new Goblin();
 3
 4goblin.printStatus();
 5wizard.castSpell(goblin::changeSize);
 6goblin.printStatus();
 7
 8wizard.castSpell(goblin::changeVisibility);
 9goblin.printStatus();
10
11wizard.undoLastSpell();
12goblin.printStatus();
13
14wizard.undoLastSpell();
15goblin.printStatus();
16
17wizard.redoLastSpell();
18goblin.printStatus();
19
20wizard.redoLastSpell();
21goblin.printStatus();

Here’s the program output:

1Goblin, [size=normal] [visibility=visible]
2Goblin, [size=small] [visibility=visible]
3Goblin, [size=small] [visibility=invisible]
4Goblin, [size=small] [visibility=visible]
5Goblin, [size=normal] [visibility=visible]
6Goblin, [size=small] [visibility=visible]
7Goblin, [size=small] [visibility=invisible]

Class diagram

alt text

Applicability

Use the Command pattern when you want to:

  • Parameterize objects by an action to perform. You can express such parameterization in a procedural language with a callback function, that is, a function that’s registered somewhere to be called 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 life independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can 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 un-execute operation that reverses the effects of a previous call to execute. The executed commands are stored in a history list. Unlimited-level undo and redo functionality is achieved by traversing this list backward and forward calling un-execute and execute, respectively.
  • Support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from the disk and re-executing them with the execute operation.
  • Structure a system around high-level operations build on primitive operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of data changes. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions.
  • Keep a history of requests.
  • Implement callback functionality.
  • Implement the undo functionality.

Known uses

Credits