Differences between revisions 2 and 12 (spanning 10 versions)
Revision 2 as of 2003-11-17 22:09:57
Size: 755
Editor: yermat
Comment:
Revision 12 as of 2003-11-22 18:26:12
Size: 309
Editor: yermat
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 See also http://dales.rmplc.co.uk/Duncan/accu/pythonpatterns.html/
Line 7: Line 7:
=== classmethod ===
----
''' pro '''
and http://www.thinkware.se/cgi-bin/thinki.cgi/PythonPatterns
Line 11: Line 9:
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.
=== GoF ===
Line 14: Line 11:
----
''' cons '''
 * SingletonProgramming
 * ProxyProgramming
Line 17: Line 14:


{{{
#!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()
}}}
=== Other ===
 
 * ["DEM"] Delegation Event Model

PatternProgramming (last edited 2008-11-15 13:59:47 by localhost)

Unable to edit the page? See the FrontPage for instructions.