策略模式

策略模式体现的是可插拔性,根据不同的策略达到目的.从而提高软件的弹性

什么是策略模式:

从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()    


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值