-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoker.py
More file actions
68 lines (57 loc) · 1.72 KB
/
Copy pathinvoker.py
File metadata and controls
68 lines (57 loc) · 1.72 KB
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
from typing import Optional
from .command import Command
class Invoker:
"""
The Invoker stores and triggers commands.
"""
def __init__(self):
self._on_start: Optional[Command] = None
self._on_finish: Optional[Command] = None
def set_on_start(self, command: Command) -> None:
self._on_start = command
def set_on_finish(self, command: Command) -> None:
self._on_finish = command
def run(self) -> list[str]:
output = []
if self._on_start:
output.append("Invoker: Starting operation...")
output.append(self._on_start.execute())
if self._on_finish:
output.append("Invoker: Finishing operation...")
output.append(self._on_finish.execute())
return output
# Example Usage:
#
# from receiver import Receiver
# from concrete_command_a import ConcreteCommandA
# from concrete_command_b import ConcreteCommandB
# from invoker import Invoker
#
# receiver = Receiver()
# invoker = Invoker()
#
# command_a = ConcreteCommandA(receiver)
# command_b = ConcreteCommandB(receiver)
#
# invoker.set_on_start(command_a)
# invoker.set_on_finish(command_b)
#
# invoker.run()
#
# Output:
# Invoker: Starting operation...
# Receiver: Action A executed.
# Invoker: Finishing operation...
# Receiver: Action B executed.
#
#
# Common Mistakes to Avoid:
#
# 1. Putting business logic inside Commands.
# - Fix: Keep logic in Receiver, commands should delegate only.
#
# 2. Using too many command classes for trivial operations.
# - Fix: Use Command Pattern only when you need undo/queue/logging/execution control.
#
# 3. Tight coupling Invoker <-> concrete commands.
# - Fix: Invoker should work with the Command interface only.