from abc import ABCMeta, abstractmethod
# 抽象策略
class Strategy(metaclass=ABCMeta):
@abstractmethod
def execute(self, data):
pass
# 具体策略
class FastStrategy(Strategy):
def execute(self, data):
print('用较快的策略处理: %s' % data)
# 具体策略
class SlowStrategy(Strategy):
def execute(self, data):
print('用较慢的策略处理: %s' % data)
# 上下文
class Context(object):
def __init__(self, strategy, data):
self.strategy = strategy
self.data = data
def set_strategy(self, strategy):
self.strategy = strategy
def do_strategy(self):
self.strategy.execute(self.data)
# Client
data = ['user1', 'user2', 'user3']
s = FastStrategy()
context = Context(s, data)
context.do_strategy()
s2 = SlowStrategy()
context.set_strategy(s2)
context.do_strategy()