python实现Memento模式,python实现memento,下面的例子通过pytho
python实现Memento模式,python实现memento,下面的例子通过pytho
下面的例子通过python的metaclass实现Memento设计模式:
class MementoMetaclass(type): cache = {} def __call__(self, *args): print "="*20 print "ClassObj:", self print "Args:", args print "="*20 cached = self.cache.get(args, None) if not cached: instance = type.__call__(self, *args) self.cache.update({args:instance}) return instance return cachedclass Foo(object): __metaclass__ = MementoMetaclass template = '' def __init__(self, arg1, arg2, arg3): self.template = arg1a = Foo(1,2,3)b = Foo(2,3,4)c = Foo(1,2,3)d = Foo(2,3,4)e = Foo(5,6,7)f = Foo(5,6,7)print id(a), id(b), id(c), id(d), id(e), id(f)
运行上面的代码,会发现,上面的例子代码中当Foo构造函数传递相同参数时对象是同一个对象。
相关内容
- Python生成字母,数字,特殊符号组成的随机密码,pyt
- Tkinter的ToolTip控件,tkintertooltip,Tkinter的Tool
- python中的filter方法,pythonfilter方法,filter函数:fil
- python自动连接ssh的代码,pythonssh代码,python自动连接ss
- Python处理命令行参数示例代码,python示例代码,下面代码
- Python实现简单的web server,pythonwebserver,这是Python实现的
- Python计算程序运行时间,python计算程序运行,使用time模
- python计算圆周率pi,python圆周率pi,from sys imp
- Python通过深拷贝实现从实例继承,python实例,Take an ins
- python刷新浪微博粉丝,python新浪博粉丝,之前有个哥们老
评论关闭