[Tutor] singleton pattern

Benoit Dupire bdupire@seatech.fau.edu
Wed, 16 May 2001 10:26:32 -0400


--------------82EB25BA2AB6BFFC2AB4A1E5
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

I would like to implement a  class dubbed "Factory " with the singleton
pattern, so that i only have a single "factory" object, that
instantiates some shared objects (pretty much in the same way than in
the Flyweight pattern)

This works

class Factory:
    def __init__(self):
        # Do something
        pass
    def __call__(self):
        return self

Factory = Factory()

which is a nice solution (not from me ! :o) )
The problem is that i already have a __call__ method implemented in my
Factory class.
Python unfortunately does not support overriding...
Solution 1 is to rename my previous __call__ method...
Solution 2 is to find another implementation

I wrote the following, but static methods do not seem to exist in Python

class Factory:
    _instance = None
    def getInstance():
        if Factory._instance== None:
            Factory._instance= Foo()
        return Factory._instance
    def setA(self, a):
        self.a = a
    def getA(self):
        return self.a

>>> b= Factory.getInstance()
Traceback (innermost last):
  File "<pyshell#77>", line 1, in ?
    b= Factory.getInstance()
TypeError: unbound method must be called with class instance 1st
argument

Do i have to implement it as a separate function ?
I think something like what is below would do the job.. but is there a
way to implement static methods ?

class Factory:
    _instance = None
    <bla - bla >

def getFactory:
    if Factory._instance ==None:
        Factory.__instance = Factory()
    return Factory.__instance



benoit




--------------82EB25BA2AB6BFFC2AB4A1E5
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
I would like to implement a&nbsp; class dubbed "Factory " with the singleton
pattern, so that i only have a single "factory" object, that instantiates
some shared objects (pretty much in the same way than in the Flyweight
pattern)<b></b>
<p>This works<b></b>
<p><tt>class Factory:</tt>
<br><tt>&nbsp;&nbsp;&nbsp; def __init__(self):</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Do something</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass</tt>
<br><tt>&nbsp;&nbsp;&nbsp; def __call__(self):</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self</tt><tt></tt>
<p><tt>Factory = Factory()</tt><b></b>
<p>which is a nice solution (not from me ! :o) )
<br>The problem is that i already have a __call__ method implemented in
my Factory class.
<br>Python unfortunately does not support overriding...
<br>Solution 1 is to rename my previous __call__ method...
<br>Solution 2 is to find another implementation
<p>I wrote the following, but static methods do not seem to exist in Python
<p><tt>class Factory:</tt>
<br><tt>&nbsp;&nbsp;&nbsp; _instance = None</tt>
<br><tt>&nbsp;&nbsp;&nbsp; def getInstance():</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if Factory._instance==
None:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Factory._instance= Foo()</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Factory._instance</tt>
<br><tt>&nbsp;&nbsp;&nbsp; def setA(self, a):</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.a = a</tt>
<br><tt>&nbsp;&nbsp;&nbsp; def getA(self):</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self.a</tt><tt></tt>
<p><tt>>>> b= Factory.getInstance()</tt>
<br><tt>Traceback (innermost last):</tt>
<br><tt>&nbsp; File "&lt;pyshell#77>", line 1, in ?</tt>
<br><tt>&nbsp;&nbsp;&nbsp; b= Factory.getInstance()</tt>
<br><tt>TypeError: unbound method must be called with class instance 1st
argument</tt>
<p>Do i have to implement it as a separate function ?
<br>I think something like what is below would do the job.. but is there
a way to implement static methods ?
<p><tt>class Factory:</tt>
<br><tt>&nbsp;&nbsp;&nbsp; _instance = None</tt>
<br><tt>&nbsp;&nbsp;&nbsp; &lt;bla - bla ></tt><tt></tt>
<p><tt>def getFactory:</tt>
<br><tt>&nbsp;&nbsp;&nbsp; if Factory._instance ==None:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Factory.__instance =
Factory()</tt>
<br><tt>&nbsp;&nbsp;&nbsp; return Factory.__instance</tt>
<br><tt></tt>&nbsp;
<br><tt></tt>&nbsp;<tt></tt>
<p><tt>benoit</tt>
<br>&nbsp;
<br>&nbsp;
<br>&nbsp;</html>

--------------82EB25BA2AB6BFFC2AB4A1E5--