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

首頁 > 學院 > 開發設計 > 正文

python優雅實現策略模式

2019-11-14 09:18:29
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海宁市| 龙州县| 新田县| 仁怀市| 辉县市| 石河子市| 通道| 芜湖市| 台湾省| 平乡县| 盐城市| 玉林市| 罗定市| 嘉黎县| 左云县| 遵化市| 龙胜| 河津市| 康保县| 神农架林区| 庐江县| 布拖县| 阿克苏市| 建湖县| 化州市| 阿图什市| 藁城市| 饶阳县| 来安县| 大丰市| 塔河县| 上栗县| 木兰县| 滦南县| 谷城县| 白河县| 巨鹿县| 河津市| 普洱| 万源市| 澄迈县|