Created
September 8, 2016 08:41
-
-
Save alessandrocucci/d0f38662256c6f09e264819171ef5284 to your computer and use it in GitHub Desktop.
Strategy Pattern in Python (v. 2.x, 3.x)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import types | |
if sys.version_info[0] > 2: # Python 3+ | |
create_bound_method = types.MethodType | |
else: | |
def create_bound_method(func, obj): | |
return types.MethodType(func, obj, obj.__class__) | |
class StrategyExample: | |
def __init__(self, func=None): | |
self.name = "Strategy Example 0" | |
if func: | |
self.execute = create_bound_method(func, self) | |
def execute(self): | |
print(self.name) | |
def executeReplacement1(self): | |
print(self.name + " from execute 1") | |
def executeReplacement2(self): | |
print(self.name + " from execute 2") | |
if __name__ == "__main__": | |
strat0 = StrategyExample() | |
strat1 = StrategyExample(executeReplacement1) | |
strat1.name = "Strategy Example 1" | |
strat2 = StrategyExample(executeReplacement2) | |
strat2.name = "Strategy Example 2" | |
strat0.execute() | |
strat1.execute() | |
strat2.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment