Differences between revisions 5 and 6
Revision 5 as of 2003-11-17 22:11:21
Size: 827
Editor: yermat
Comment:
Revision 6 as of 2003-11-17 22:14:15
Size: 85
Editor: yermat
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
= Singleton = = Pattern Programming =
Line 5: Line 5:
See also http://c2.com/cgi/wiki?PythonSingleton and http://www.python.org/workshops/1997-10/proceedings/savikko.html

=== 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 '''



{{{
#!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()
}}}
=== GoF ===
 * Singleton

Pattern Programming

GoF

  • Singleton

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

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