-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-2-command.vala
74 lines (57 loc) · 1.2 KB
/
3-2-command.vala
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
class Bulb {
public void turn_on () {
print ("Bulb has been lit\n");
}
public void turn_off () {
print ("Darkness!\n");
}
}
interface Command {
public abstract void execute ();
public abstract void undo ();
public abstract void redo ();
}
class TurnOn : Command {
protected Bulb bulb;
public TurnOn (Bulb bulb) {
this.bulb = bulb;
}
public void execute () {
bulb.turn_on ();
}
public void undo () {
bulb.turn_off ();
}
public void redo () {
execute ();
}
}
class TurnOff : Command {
protected Bulb bulb;
public TurnOff (Bulb bulb) {
this.bulb = bulb;
}
public void execute () {
bulb.turn_off ();
}
public void undo () {
bulb.turn_on ();
}
public void redo () {
execute ();
}
}
class RemoteControl {
public void submit (Command command) {
command.execute ();
}
}
public int main (string[] args) {
var bulb = new Bulb ();
var turn_on = new TurnOn (bulb);
var turn_off= new TurnOff (bulb);
var remote = new RemoteControl ();
remote.submit (turn_on);
remote.submit (turn_off);
return 0;
}