Refer to Other blogs: http://ghostfromheaven.iteye.com/blog/1562618
#-*-encoding=utf-8-*-Print '----------------------Method 1--------------------------' #Method 1, implement the __new__ method#and when you bind an instance of a class to the class variable _instance,#If Cls._instance is None, the class has not been instantiated, instantiates the class, and returns#if Cls._instance is not none, return directly to Cls._instanceclassSingleton (object):def __new__(CLS, *args, * *kw):if notHasattr (CLS,'_instance'): Orig=Super (Singleton, CLS) cls._instance= orig.__new__(CLS, *args, * *kw)returncls._instanceclassMyClass (Singleton): a= 1 One=MyClass ()=MyClass () two.a= 3PrintOne.a#3#One and both are identical and can be detected with ID (), = =, isPrintID (one)#29097904PrintID (both)#29097904Printone = = Both#TruePrintOne is Both#True Print '----------------------Method 2--------------------------' #Method 2, shared attribute; the so-called Singleton is all references (instances, objects) have the same state (attributes) and behavior (methods)#all instances of the same class naturally have the same behavior (methods),#just make sure that all instances of the same class have the same state (attribute)#The simplest and most straightforward way to share properties for all instances is to __dict__ the same dictionary (reference) (Dict)#can see: http://code.activestate.com/recipes/66531/classBorg (object): _state= {} def __new__(CLS, *args, * *kw): OB= Super (Borg, CLS).__new__(CLS, *args, * *kw) ob.__dict__=cls._statereturnobclassMyClass2 (Borg): a= 1 One=MyClass2 ()=MyClass2 ()#One and two are two different objects, id, = =, is comparison results can be seenTWO.A = 3PrintOne.a#3PrintID (one)#28873680PrintID (both)#28873712Printone = = Both#FalsePrintOne is Both#False#but one and both have the same (same __dict__ attribute), see:PrintID (one.__dict__) #30104000PrintID (both.__dict__) #30104000 Print '----------------------Method 3--------------------------' #Method 3: Essentially an upgrade (or premium) version of Method 1#advanced Python usage with __metaclass__ (META Class)classSingleton2 (type):def __init__(CLS, name, bases, Dict): Super (Singleton2, CLS).__init__(name, bases, dict) cls._instance=Nonedef __call__(CLS, *args, * *kw):ifCls._instance isnone:cls._instance= Super (Singleton2, CLS).__call__(*args, * *kw)returncls._instanceclassMyClass3 (object):__metaclass__=Singleton2 One=MYCLASS3 ()=MyClass3 () two.a= 3PrintOne.a#3PrintID (one)#31495472PrintID (both)#31495472Printone = = Both#TruePrintOne is Both#True Print '----------------------Method 4--------------------------' #Method 4: Also the upgrade (advanced) version of Method 1,#using adorners (decorator),#This is a more pythonic, more elegant approach,#The Singleton class itself does not know that he is a singleton, because he (his own code) is not a single casedefSingleton (CLS, *args, * *kw): Instances= {} def_singleton ():ifCls not inchInstances:instances[cls]= CLS (*args, * *kw)returnInstances[cls]return_singleton @singletonclassMyClass4 (object): a= 1def __init__(Self, x=0): self.x=x One=MyClass4 ()=MyClass4 () two.a= 3PrintOne.a#3PrintID (one)#29660784PrintID (both)#29660784Printone = = Both#TruePrintOne is Both#Trueone.x = 1Printone.x#1Printtwo.x#1
Python Design Pattern Learning (3): Singleton pattern