#单例模式的父类,单例模式的类可以从这个类继承
class Singleton(object):
    def __new__(cls,*args,**kw):
        if not hasattr(cls,"_instance"):
            orig = super(Singleton,cls)
            cls._instance = orig.__new__(cls,*args,**kw)
        return cls._instance  

#---------------------------------------

#需要使用单例模式的类时,可以从Singleton继承

class Parameter(Singleton):

...

...

...

#--------------------------------------

p1 = Parameter()

p2 = Parameter()

#p1和p2其实是同一个实例

posted on 2011-05-09 15:42  WordAnyTime  阅读(807)  评论(2编辑  收藏  举报