-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.py
More file actions
43 lines (36 loc) · 1.25 KB
/
Copy pathcontext.py
File metadata and controls
43 lines (36 loc) · 1.25 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
from .strategy import Strategy
class Context:
"""
The Context maintains a reference to a Strategy object and delegates
the work to it instead of implementing multiple algorithm variations.
"""
def __init__(self, strategy: Strategy):
self._strategy: Strategy = strategy
def set_strategy(self, strategy: Strategy) -> None:
self._strategy = strategy
def execute_strategy(self, a: int, b: int) -> int:
return self._strategy.execute(a, b)
# Example Usage:
# from context import Context
# from concrete_strategy_a import ConcreteStrategyA
# from concrete_strategy_b import ConcreteStrategyB
#
# context = Context(ConcreteStrategyA())
# print(context.execute_strategy(5, 3))
# # Output: 8
#
# context.set_strategy(ConcreteStrategyB())
# print(context.execute_strategy(5, 3))
# # Output: 15
#
#
# Common Mistakes to Avoid:
#
# 1. Adding conditional logic inside the Context instead of strategies.
# - Fix: Move algorithm variations into separate Strategy classes.
#
# 2. Creating too many strategies for trivial operations.
# - Fix: Only use Strategy when you truly need interchangeable algorithms.
#
# 3. Forgetting to make Strategy methods consistent.
# - Fix: Ensure all strategies implement the same method signature.