-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathstrategy.py
More file actions
49 lines (31 loc) Β· 1.19 KB
/
Copy pathstrategy.py
File metadata and controls
49 lines (31 loc) Β· 1.19 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
"""
Strategy pattern - we have a default method, we can change its behaviour with another function at run time.
It is easy to implement with Python with its build-in "types" module.
"""
import types
class Strategy:
"""The Strategy Pattern class"""
def __init__(self, function=None):
self.name = "Default Strategy"
# If a reference to a function is provided, replace the execute() method with the given function
if function:
self.execute = types.MethodType(function, self)
def execute(self): # This gets replaced by another version if another strategy is provided.
"""The default method that prints the name of the strategy being used"""
print("{} is used!".format(self.name))
def strategy_one(self):
print("{} is used to execute method 1".format(self.name))
def strategy_two(self):
print("{} is used to execute method 2".format(self.name))
def main():
# Let's create our default strategy
s0 = Strategy()
s0.execute()
s1 = Strategy(strategy_one)
s1.name = "Strategy One"
s1.execute()
s2 = Strategy(strategy_two)
s2.name = "Strategy Two"
s2.execute()
if __name__ == "__main__":
main()