@@ -22,7 +22,7 @@ def __init__(self, light):
22
22
self .light = light
23
23
24
24
def execute (self ):
25
- self .level = self .light .getLevel ()
25
+ self .level = self .light .get_level ()
26
26
self .light .on ()
27
27
28
28
def undo (self ):
@@ -34,7 +34,7 @@ def __init__(self, light):
34
34
self .light = light
35
35
36
36
def execute (self ):
37
- self .level = self .light .getLevel ()
37
+ self .level = self .light .get_level ()
38
38
self .light .off ()
39
39
40
40
def undo (self ):
@@ -44,6 +44,7 @@ def undo(self):
44
44
class Light ():
45
45
def __init__ (self , location :str ):
46
46
self .location = location
47
+ self .level = 0
47
48
48
49
def on (self ):
49
50
self .level = 100
@@ -55,7 +56,7 @@ def off(self):
55
56
56
57
def dim (self , level ):
57
58
self .level = level
58
- if level == 0 :
59
+ if self . level == 0 :
59
60
self .off ()
60
61
else :
61
62
print (f"Light is dimmed to { self .level } %" )
@@ -64,3 +65,56 @@ def get_level(self):
64
65
return self .level
65
66
66
67
68
+ class RemoteControlWithUndo ():
69
+ """The Invoker"""
70
+
71
+ def __init__ (self ) -> None :
72
+ self .on_commands = [NoCommand () for _ in range (7 )]
73
+ self .off_commands = [NoCommand () for _ in range (7 )]
74
+ self .undo_command = NoCommand ()
75
+
76
+ def set_command (self , i , on_command , off_command ):
77
+ self .on_commands [i ] = on_command
78
+ self .off_commands [i ] = off_command
79
+
80
+ def on_button_was_pushed (self , i ):
81
+ self .on_commands [i ].execute ()
82
+ self .undo_command = self .on_commands [i ]
83
+
84
+ def off_button_was_pushed (self , i ):
85
+ self .off_commands [i ].execute ()
86
+ self .undo_command = self .off_commands [i ]
87
+
88
+ def undo_button_was_pushed (self ):
89
+ self .undo_command .undo ()
90
+
91
+ def __str__ (self ) -> str :
92
+ buffer = []
93
+ buffer .append ("\n ------ Remote Control -------\n " )
94
+ for i , (on_command , off_command ) in enumerate (zip (self .on_commands , self .off_commands )):
95
+ buffer .append (f"[slot { i } ] { on_command .__class__ .__name__ } " +
96
+ f" { off_command .__class__ .__name__ } \n " )
97
+
98
+ buffer .append (f"[undo] { self .undo_command .__class__ .__name__ } \n " )
99
+ return "" .join (buffer )
100
+
101
+
102
+ def remote_loader ():
103
+ remoteControl = RemoteControlWithUndo ()
104
+ living_room_light = Light ("Living Room" )
105
+ living_room_light_on = LightOnCommand (living_room_light )
106
+ living_room_light_off = LightOffCommand (living_room_light )
107
+
108
+ remoteControl .set_command (0 , living_room_light_on , living_room_light_off )
109
+
110
+ remoteControl .on_button_was_pushed (0 )
111
+ remoteControl .off_button_was_pushed (0 )
112
+ print (remoteControl )
113
+ remoteControl .undo_button_was_pushed ()
114
+ remoteControl .off_button_was_pushed (0 )
115
+ remoteControl .on_button_was_pushed (0 )
116
+ print (remoteControl )
117
+ remoteControl .undo_button_was_pushed ()
118
+
119
+ if __name__ == '__main__' :
120
+ remote_loader ()
0 commit comments