国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python優雅實現策略模式

2019-11-11 07:53:08
字體:
來源:轉載
供稿:網友

優雅實現策略模式; 在awesome-python上有一個關于模式設計的包,對包中的代碼做了一點修改。

# -*- coding: utf-8 -*-"""http://stackoverflow.com/questions/963965/how-is-this-strategy-pattern -written-in-python-the-sample-in-wikipediaIn most of other languages Strategy pattern is implemented via creating somebase strategy interface/abstract class and subclassing it with a number ofconcrete strategies (as we can see athttp://en.wikipedia.org/wiki/Strategy_pattern), however Python supportshigher-order functions and allows us to have only one class and injectfunctions into it's instances, as shown in this example."""import typesclass StrategyExample:    def __init__(self, func=None):        self.name = 'Strategy Example 0'        if func is not None:            self.select(func)    def __call__(self, arg=''):   # special method for execute        self.execute()    def execute(self):        

輸出為

nothingnothingStrategy Example 1 from execute 1Strategy Example 2 from execute 2Strategy Example 2 from execute 1Strategy Example x from execute 2


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 哈密市| 保定市| 泗阳县| 元朗区| 建湖县| 治县。| 浮梁县| 额尔古纳市| 凤翔县| 静安区| 崇明县| 旺苍县| 富裕县| 齐齐哈尔市| 蒙城县| 莆田市| 北辰区| 永定县| 错那县| 自治县| 平凉市| 滕州市| 天台县| 定结县| 秀山| 珠海市| 临洮县| 遵化市| 虎林市| 鹤峰县| 九龙县| 屯昌县| 威海市| 常宁市| 吴江市| 霍城县| 吕梁市| 长岛县| 丹阳市| 邢台市| 垫江县|