-
Notifications
You must be signed in to change notification settings - Fork 206
/
strategy.py
54 lines (33 loc) · 1.11 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
class StrategyExample:
def __init__(self, func=None):
if func:
self.execute = func
def execute(self):
print "Original execution"
def executeReplacement1(self):
print "Strategy 1"
def executeReplacement2(self):
print "Strategy 2"
if __name__ == "__main__":
strat0 = StrategyExample()
strat1 = StrategyExample(executeReplacement1)
strat2 = StrategyExample(executeReplacement2)
strat0.execute()
strat1.execute()
strat2.execute()
# -------------------- With classes --------------------
class AUsefulThing(object):
def __init__(self, aStrategicAlternative):
self.howToDoX = aStrategicAlternative
def doX(self, someArg):
self. howToDoX.theAPImethod(someArg, self)
class StrategicAlternative(object):
pass
class AlternativeOne(StrategicAlternative):
def theAPIMethod(self, someArg, theUsefulThing):
pass # an implementation
class AlternativeTwo(StrategicAlternative):
def theAPImethod(self, someArg, theUsefulThing):
pass # another implementation
t = AUsefulThing(AlternativeOne())
t.doX('arg')