Action, Transaction
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
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]
Use the Command pattern when you want to: