-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommandPatternLambda.java
75 lines (58 loc) · 2.05 KB
/
CommandPatternLambda.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
/*
* 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 CommandPatternLambda {
public static void main (String args[]) {
Lightbulb bulb = new Lightbulb();
RemoteController remote = new RemoteController();
remote.setCommand(bulb::turnOn);
remote.execute();
remote.setCommand(bulb::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();
}
/*
* 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();
}
}