-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommandPattern.java
136 lines (106 loc) · 3.01 KB
/
CommandPattern.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* This program implements the Command design pattern the following way:
* A simple prgram with a Remote Controller that makes a lightbulb turn on and off.
* The idea is to make it as less coupled as possible, and to be able to have freedom
* working with the functions in different ways.
* Based on https://dzone.com/articles/design-patterns-command and
* https://github.com/kamranahmedse/design-patterns-for-humans
*/
/*
* This is the main class to execute the program. It corresponds to the Client that uses
* the Invoker (RemoteController) to use the Receiver's (Lightbulb) actions through Commands.
* It also creates Commands, Receiver, and Invoker, setting them appropriately
*/
public class CommandPattern {
public static void main (String args[]) {
Lightbulb bulb = new Lightbulb();
TurnOnCommand turnOn = new TurnOnCommand(bulb);
TurnOffCommand turnOff = new TurnOffCommand(bulb);
RemoteController remote = new RemoteController();
remote.setCommand(turnOn);
remote.execute();
remote.setCommand(turnOff);
remote.execute();
}
}
/*
* This class represents the Lightbulb, which has the desired functions to execute
* Hence, this is equivalent to be the Receiver class
*/
class Lightbulb {
public void turnOn() {
System.out.println("Bulb has been lit");
}
public void turnOff() {
System.out.println("Darkness!!");
}
}
/*
* This interface generalizes the Command objects that encapsulate and handle function
* calls to the Lightbulb (the Receiver). The desired handlers in this case are:
* execute (perform the implementation-specific logic), redo (perform last action again),
* and undo (to reverse the execution of the last action)
*/
interface Command {
public void execute();
public void redo();
public void undo();
}
/*
* This class represents the Turn On Command of the Controller, which has to call the
* Lightbulb's Turn On actions
*/
class TurnOnCommand implements Command {
private Lightbulb bulb;
public TurnOnCommand (Lightbulb bulb){
this.bulb = bulb;
}
@Override
public void execute() {
bulb.turnOn();
}
@Override
public void redo() {
this.execute();
}
@Override
public void undo() {
bulb.turnOff();
}
}
/*
* This class represents the Turn Off Command of the Controller, which has to call the
* Lightbulb's Turn Off actions
*/
class TurnOffCommand implements Command {
private Lightbulb bulb;
public TurnOffCommand(Lightbulb bulb){
this.bulb = bulb;
}
@Override
public void execute() {
bulb.turnOff();
}
@Override
public void redo() {
this.execute();
}
@Override
public void undo() {
bulb.turnOn();
}
}
/*
* This class represents the Remote Controller with Commands that interact with the lightbulb.
* Hence, it is an Invoker (the client's interface to performing Lightbulb's operations
* without directly touching the Lightbulb object and functions).
*/
class RemoteController {
Command command;
public void setCommand(Command command) {
this.command = command;
}
public void execute() {
command.execute();
}
}