Size: 755
Comment:
|
Size: 102
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
= Singleton = | = Pattern Programming = |
Line 5: | Line 5: |
Voyez aussi http://c2.com/cgi/wiki?PythonSingleton | === GoF === |
Line 7: | Line 7: |
=== classmethod === ---- ''' pro ''' You can use both as simple class or as a singleton. You do not need to write code for each class you want to act as singleton. ---- ''' cons ''' |
* SingletonProgramming |
Line 18: | Line 10: |
{{{ #!python # Code is Public Domain. class Singleton: _singleton = None def getSingleton(cls): if not Singleton._singleton: Singleton._singleton = cls() return Singleton._singleton getSingleton = classmethod(getSingleton) class Test(Singleton): def test(self): print self.__class__,id(self) t1 = Test.getSingleton() t2 = Test.getSingleton() t1.test() t2.test() }}} |