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

首頁 > 編程 > Python > 正文

python優雅實現策略模式

2019-11-11 07:30:56
字體:
來源:轉載
供稿:網友

優雅實現策略模式; 在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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 韩城市| 阳东县| 山东省| 乌兰浩特市| 南阳市| 文安县| 桃园县| 新干县| 自治县| 青岛市| 牟定县| 义乌市| 招远市| 中牟县| 古丈县| 丘北县| 璧山县| 新宁县| 鹤峰县| 寻乌县| 苗栗县| 万年县| 安龙县| 会泽县| 太和县| 温州市| 古浪县| 莱西市| 百色市| 乐亭县| 象山县| 鄂托克前旗| 安庆市| 兴业县| 肥城市| 布尔津县| 奉节县| 甘谷县| 北辰区| 社旗县| 紫云|