python版本的策略模式
生活随笔
收集整理的這篇文章主要介紹了
python版本的策略模式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
策略提供一種用多個(gè)行為中的一個(gè)行為來配置一個(gè)類的方法。其實(shí)就是應(yīng)用于同一個(gè)問題的不同解決方案
# -*- coding:UTF-8 -*- import abcclass Strategy(metaclass=abc.ABCMeta):@abc.abstractmethoddef algorithm_interface(self):passclass ConcreteStrategyA(Strategy):def algorithm_interface(self):print('算法A實(shí)現(xiàn)')class ConcreteStrategyB(Strategy):def algorithm_interface(self):print('算法B實(shí)現(xiàn)')class ConcreteStrategyC(Strategy):def algorithm_interface(self):print('算法C實(shí)現(xiàn)')class Context:def __init__(self,strategy):self.strategy=strategydef 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()?
轉(zhuǎn)載于:https://www.cnblogs.com/gjinwei/p/8351135.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的python版本的策略模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: unix的sed 用法介绍
- 下一篇: 论思维方式的重要性