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

首頁 > 編程 > Python > 正文

python優雅實現策略模式

2019-11-11 07:25: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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳泉市| 淮阳县| 逊克县| 万载县| 内江市| 凤山县| 乐平市| 安阳县| 永嘉县| 疏勒县| 奎屯市| 扶沟县| 朝阳市| 香格里拉县| 梧州市| 出国| 高要市| 安国市| 齐齐哈尔市| 永胜县| 皋兰县| 越西县| 墨江| 福安市| 电白县| 武功县| 登封市| 莒南县| 金坛市| 绥化市| 上栗县| 塘沽区| 株洲市| 香河县| 台前县| 金乡县| 大冶市| 邯郸县| 南和县| 建阳市| 天镇县|