Command

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18

"""
This is a Command Class
"""
class Command(ABC):
    """
    The Command interface declares a method for executing a command.
    """

    @abstractmethod
    def execute(self) -> None:
        pass


    @abstractmethod
    def undo(self) -> None:
        pass
		

LightOnCommand

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17

class LightOnCommand(Command):
    """ 
    We need cohersion the Receiver and implement real command object 
    """


    def __init__(self, receiver: Receiver, a: str) -> None:
        self._receiver = receiver
        self._a = a

    def execute(self) -> None:
        self._receiver.on
        # print()

    def undo(self) -> None:
        self._receiver.off

LightOffCommand

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17

class LightOffCommand(Command):
    """ 
    We need cohersion the Receiver and implement real command object 
    """


    def __init__(self, receiver: Receiver, a: str) -> None:
        self._receiver = receiver
        self._a = a

    def execute(self) -> None:
        self._receiver.off
        # print()

    def undo(self) -> None:
        self._receiver.on

Receiver

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Receiver(object):
    
    """
    The Receiver classes contain some important business logic. They know how to
    perform all kinds of operations, associated with carrying out a request. In
    fact, any class may serve as a Receiver.

    implement turn on and turn off Receiver 
    """

    def on(self, a: str) -> None:
        print(f"\n turn on the Light ({a}.)", end="" )

    
    
    def off(self, a: str) -> None:
        print(f"\n turn off the Light ({a}.)", end="" )

Invoker

 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
class NoCommand(Command):

    def execute(self):
        print('Command Not Found')

    def undo(self):
        print('Command Not Found')


class Invoker:
    """
    The Invoker is associated with one or several commands. It sends a request
    to the command.
    """

    """
    Initialize commands.
    """


    def __init__(self) -> None:

         self.on_commands = [NoCommand() for i in range(7)]
         self.off_commands = [NoCommand() for i in range(7)]
         self.undo_command = None

    def set_command(self, slot, on_command, off_command):
       
        self.on_commands[slot] = on_command
        self.off_commands[slot] = off_command

    def on_button_was_pressed(self, slot):
        command = self.on_commands[slot]
        command.execute()
        self.undo_command = command

    def off_button_was_pressed(self, slot):
        command = self.off_commands[slot]
        command.execute()
        self.undo_command = command

    def undo_button_was_pressed(self):
        self.undo_command.undo()
    
    def __str__(self):
        for i in range(7):
            print('[slot %d] %s %s' % (i,
                                       self.on_commands[i].__class__.__name__,
                                       self.off_commands[i].__class__.__name__))
        return ''
    

Client

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18

if __name__ == "__main__":
    """
    The client code can parameterize an invoker with any commands.
    """

    invoker = Invoker()

    living_room_light = Receiver()
    living_room_light_on = LightOnCommand(living_room_light, "Living Room Light")
    living_room_light_off = LightOffCommand(living_room_light, "Living Room Light")


    invoker.set_command(0,living_room_light_on, living_room_light_off )

    print(invoker)
    invoker.on_button_was_pressed(0)
    invoker.off_button_was_pressed(0)
  • Alreadu finish the implement: Turn on/off the light
  • We need to have more scenarios.
Class Name Description Abstract
Command Interface which need implemented by sub-class Yes
LightOnCommand Some commands can implement simple operations on their own. No
LightOffCommand No
Receiver The Receiver classes contain some important business logic. Yes
Invoker The Invoker is associated with one or several commands. No
Client The client code can parameterize an invoker with any commands. No

[Source Code]

Achilles’ Blog