策略模式体现的是可插拔性,根据不同的策略达到目的.从而提高软件的弹性
什么是策略模式:
从FP角度看就是对同一函数签名的不同实现体的选择
'''Created on Feb 4, 2016 @author: Linux'''
import types
if __name__ == '__main__':
print("Hello World!")
class StrategyExample(object):
def __init__(self,func=None):
self.name = "Strategy Example 0"
if func is not None:
self.execute = types.MethodType(func,self)
def execute(self):
print(self.name)
def excute_replacement1(self):
print(self.name)
def excute_replacement2(self):
print(self.name)
strat0 = StrategyExample()
strat1 = StrategyExample(excute_replacement1)
strat1.name = 'Strategy Example 1'
strat2 = StrategyExample(excute_replacement2)
strat2.name = 'Strategy Example 2'
strat0.execute()
strat1.execute()
strat2.execute()