-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.py
56 lines (37 loc) · 1.39 KB
/
strategy.py
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
from abc import ABC, abstractmethod
class Strategy(ABC):
@abstractmethod
def execute(self, data):
pass
class ConcreteStrategyA(Strategy):
def execute(self, data):
return f"{self.__class__.__name__} with data '{data}'"
class ConcreteStrategyB(Strategy):
def execute(self, data):
return f"{self.__class__.__name__} with data '{data}'"
class ConcreteStrategyC(Strategy):
def execute(self, data):
return f"{self.__class__.__name__} with data '{data}'"
class Context:
def __init__(self, strategy: Strategy):
self._strategy = strategy
def set_strategy(self, strategy: Strategy):
self._strategy = strategy
def execute_strategy(self, data):
return self._strategy.execute(data)
def execute_with_specific_strategy(self, data, strategy: Strategy):
self.set_strategy(strategy)
return self.execute_strategy(data)
if __name__ == "__main__":
data = "Example"
# Strategy A
context = Context(ConcreteStrategyA())
print(context.execute_strategy(data))
# Switch to Strategy B
context.set_strategy(ConcreteStrategyB())
print(context.execute_strategy(data))
# Switch Strategy C
context.set_strategy(ConcreteStrategyC())
print(context.execute_strategy(data))
# Switch and Execute in one Method
print(context.execute_with_specific_strategy(data, ConcreteStrategyA()))