策略模式核心:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。(算法封装类传参)
class Strategy(object):
def plus(self):
pass
class NoOneDiscountStrategy(Strategy):
def plus(self):
super(Strategy, self)
print("No .1 Strategy")
class NoTwoDisCountStrategy(Strategy):
def plus(self):
super(Strategy, self)
print("No .2 Strategy")
class DiscountContest(object):
def __init__(self, strategy):
self.__strategy = strategy
def setStrategy(self, strategyset):
self.__strategy = strategyset
def show_exceute(self):
self.__strategy.plus()
if __name__ == "__main__":
one = NoOneDiscountStrategy()
two = NoTwoDisCountStrategy()
discontent = DiscountContest(one)
discontent.show_exceute()
discontent.setStrategy(two)
discontent.show_exceute()