carfield.com.hk NewSingleton.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c01:NewSingleton.py class OnlyOne(object): class __OnlyOne: def __init__(self): self.val = None def __str__(self): return `self` + self.val instance = None def __new__(cls): # __new__ always a classmethod if not OnlyOne.instance: OnlyOne.instance = OnlyOne.__OnlyOne() return OnlyOne.instance def __getattr__(self, name): return getattr(self.instance, name) def __setattr__(self, name): return setattr(self.instance, name) x = OnlyOne() x.val = 'sausage' print x y = OnlyOne() y.val = 'eggs' print y z = OnlyOne() z.val = 'spam' print z print x print y #&lt;hr&gt; output = ''' &lt;__main__.__OnlyOne instance at 0x00798900&gt;sausage &lt;__main__.__OnlyOne instance at 0x00798900&gt;eggs &lt;__main__.__OnlyOne instance at 0x00798900&gt;spam &lt;__main__.__OnlyOne instance at 0x00798900&gt;spam &lt;__main__.__OnlyOne instance at 0x00798900&gt;spam '''Aa Alex Martelli makes the observation that what we really want with a Singleton is to have a single set of state data for all objects. That is, you could create as many objects as you want and as long as they all refer to the same state information then you achieve the effect of Singleton. He accomplishes this with what he calls the Borg7, which is accomplished by setting all the __dict__s to the same static piece of storage: Add Comment #: c01:BorgSingleton.py # Alex Martelli's 'Borg' class Borg: _shared_state = {} def __init__(self): self.__dict__ = self._shared_state class Singleton(Borg): def __init__(self, arg): Borg.__init__(self) self.val = arg def __str__(self): return self.val x = Singleton('sausage') print x y = Singleton('eggs') print y z = Singleton('spam') print z print x print y print `x` print `y` print `z` output = ''' sausage eggs spam spam spam &lt;__main__.Singleton instance at 0079EF2C&gt; &lt;__main__.Singleton instance at 0079E10C&gt; &lt;__main__.Singleton instance at 00798F9C&gt; ''' #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z SingletonPattern.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c01:SingletonPattern.java // The Singleton design pattern: you can // never instantiate more than one. // Since this isn't inherited from a Cloneable // base class and cloneability isn't added, // making it final prevents cloneability from // being added through inheritance: final class Singleton { private static Singleton s = new Singleton(47); private int i; private Singleton(int x) { i = x; } public static Singleton getReference() { return s; } public int getValue() { return i; } public void setValue(int x) { i = x; } } public class SingletonPattern { public static void main(String[] args) { Singleton s = Singleton.getReference(); System.out.println(s.getValue()); Singleton s2 = Singleton.getReference(); s2.setValue(9); System.out.println(s.getValue()); try { // Can't do this: compile-time error. // Singleton s3 = (Singleton)s2.clone(); } catch(Exception e) { e.printStackTrace(System.err); } } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z SingletonPattern.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c01:SingletonPattern.py class OnlyOne: class __OnlyOne: def __init__(self, arg): self.val = arg def __str__(self): return `self` + self.val instance = None def __init__(self, arg): if not OnlyOne.instance: OnlyOne.instance = OnlyOne.__OnlyOne(arg) else: OnlyOne.instance.val = arg def __getattr__(self, name): return getattr(self.instance, name) x = OnlyOne('sausage') print x y = OnlyOne('eggs') print y z = OnlyOne('spam') print z print x print y print `x` print `y` print `z` output = ''' &lt;__main__.__OnlyOne instance at 0076B7AC&gt;sausage &lt;__main__.__OnlyOne instance at 0076B7AC&gt;eggs &lt;__main__.__OnlyOne instance at 0076B7AC&gt;spam &lt;__main__.__OnlyOne instance at 0076B7AC&gt;spam &lt;__main__.__OnlyOne instance at 0076B7AC&gt;spam &lt;__main__.OnlyOne instance at 0076C54C&gt; &lt;__main__.OnlyOne instance at 0076DAAC&gt; &lt;__main__.OnlyOne instance at 0076AA3C&gt; ''' #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z makefile 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="" rows="16" cols="100"># From Thinking in Patterns (with Java) by Bruce Eckel # At http://www.BruceEckel.com # (c)2001 Bruce Eckel # Copyright notice in Copyright.txt # Automatically-generated MAKEFILE # For examples in directory .\c01 # using the JDK 1.3 compiler # Invoke with: make HOME := ../ ifndef MAKECMDGOALS MAKECMDGOALS := javac endif # Command.com is too weak to build this under Windows NT/2000: ifeq ($(OS),Windows_NT) COMSPEC=$(SYSTEMROOT)\system32\cmd.exe endif ifneq ($(MAKECMDGOALS),clean) include $(HOME)/$(MAKECMDGOALS).mac endif .SUFFIXES : .class .java .java.class : $(JVC) $(JVCFLAGS) $&lt; javac: \ SingletonPattern.class jikes: \ SingletonPattern.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif SingletonPattern.class: SingletonPattern.java </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z