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构造函数传递相同参数时对象是同一个对象。

评论关闭