# -*- coding:UTF-8 -*- import abc class Strategy(metaclass=abc.ABCMeta): @abc.abstractmethod def algorithm_interface(self): pass class ConcreteStrategyA(Strategy): def algorithm_interface(self): print(‘算法A实现‘) class ConcreteStrategyB(Strategy): def algorithm_interface(self): print(‘算法B实现‘) class ConcreteStrategyC(Strategy): def algorithm_interface(self): print(‘算法C实现‘) class Context: def __init__(self,strategy): self.strategy=strategy def context_interface(self): self.strategy.algorithm_interface() if __name__=="__main__": context = Context(ConcreteStrategyA()) context.context_interface() context = Context(ConcreteStrategyB()) context.context_interface() context = Context(ConcreteStrategyC()) context.context_interface()
原文地址:https://www.cnblogs.com/gjinwei/p/8351135.html
时间: 2024-11-17 15:38:44