-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommand.py
More file actions
41 lines (27 loc) · 939 Bytes
/
Copy pathcommand.py
File metadata and controls
41 lines (27 loc) · 939 Bytes
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
from abc import ABC, abstractmethod
class Command(ABC):
def __init__(self, command_id: int):
self.command_id = command_id
@abstractmethod
def execute(self):
pass
class OrderAddCommand(Command):
def execute(self):
print(f"Adding order with id {self.command_id}")
class OrderPayCommand(Command):
def execute(self):
print(f"Paying for order with id {self.command_id}")
class CommandProcessor:
queue = []
def add_to_queue(self, command: Command):
self.queue.append(command)
def process_commands(self):
[item.execute() for item in self.queue]
self.queue = []
if __name__ == '__main__':
processor = CommandProcessor()
processor.add_to_queue(OrderAddCommand(1))
processor.add_to_queue(OrderAddCommand(2))
processor.add_to_queue(OrderPayCommand(1))
processor.add_to_queue(OrderPayCommand(2))
processor.process_commands()