策略模式的python实现,相对于C++实现而言,显得更加简单。
#! /usr/bin/python
class Strategy(object):
def do_action(self):
pass
class ConcreateStrategyA(Strategy):
def do_action(self):
print "A do action"
class ConcreateStrategyB(Strategy):
def do_action(self):
print "B do action"
class Context(object):
def __init__(self, strategy):
self.strategy = strategy
def take_action(self):
self.strategy.do_action()
if __name__ == '__main__':
s1 = ConcreateStrategyA()
c = Context(s1)
c.take_action()
s2 = ConcreateStrategyB()
c = Context(s2)
c.take_action()
关于策略模式,更多信息请参考;
http://blog.csdn.net/lanyang123456/article/details/70476450