#单例模式的父类,单例模式的类可以从这个类继承
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其实是同一个实例
浙公网安备 33010602011771号