python设计模式之命令调度模式
2015-07-29 18:24
746 查看
命令调度模式
设想一个问题,假如你经常搞web框架,你知道GET/POST/PUT/DELETE,当你想确定使用那种方式工作你需要大概这样:if command == 'get': get() elif command == 'put': put() else: error()
抑或写一个字典作为调度的方式:
dispatch_table = { 'get': get, 'put': put, } if dispatch_table.has_key(command): func = dispatch_table[command] func() else: error()
其实呢?python已经自带了很多这样的功能的东西,Guido van Rossum把这个模式叫做Command Dispatch
喏 这就是,只是你没有意识到
def greet(who): print "Hello %s" % who greet_command = lambda: greet("World") greet_command()
还有python自带的库cmd 还是上面的例子
class Dispatcher: def do_get(self): ... def do_put(self): ... def error(self): ... def dispatch(self, command): mname = 'do_' + command if hasattr(self, mname): method = getattr(self, mname) method() else: self.error()
python的例子
我这里实现了一个封装pymongo的类,通过工厂方法模式执行insert/update/find操作,我这里只实现个效果
from pymongo import MongoClient class MongoPack(object): def __init__(self, db): self.db = db # 通过指定kind标识操作的种类:find/insert/update def operation(self, coll, kind, **kwargs): return getattr(self, kind)(coll, **kwargs) # 当指定kind为'find'执行这个方法 def find(self, coll, **kwargs): return self.db[coll].find(kwargs) if __name__ == '__main__': db = MongoClient()['test'] d = MongoPack(db) print d.operation('test', 'find', id=1)[0]
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- PropertyChangeListener简单理解
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- 什么是设计模式
- 设计模式之创建型模式 - 特别的变量问题
- 设计模式之创建型模式
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定