<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: singleton code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 23:08:41 GMT</pubDate>
    <description>DZone Snippets: singleton code</description>
    <item>
      <title>Java Singleton Template for Eclipse</title>
      <link>http://snippets.dzone.com/posts/show/3618</link>
      <description>This is a template for easily creating an implementation of the Singleton Pattern on Eclipse. Open Window-&gt;Preferences-&gt;Java-&gt;Editor-&gt;Templates click on New and insert the code below on the pattern text area; add a name (I suggest "singleton") - whenever you type this name and press Ctrl+Space the code will be inserted in your class - and you're good to go.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;private static ${enclosing_type} instance;&lt;br /&gt;&lt;br /&gt;private ${enclosing_type}(){}&lt;br /&gt;&lt;br /&gt;public static ${enclosing_type} getInstance(){&lt;br /&gt;	if(null == instance){&lt;br /&gt;		instance = new ${enclosing_type}();&lt;br /&gt;	}&lt;br /&gt;	return instance;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Mar 2007 19:42:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3618</guid>
      <author>marcelotmelo (Marcelo Melo)</author>
    </item>
    <item>
      <title>Singleton</title>
      <link>http://snippets.dzone.com/posts/show/3519</link>
      <description>The proper way of implementing a Singleton.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class MySingleton {&lt;br /&gt;    private static final MySingleton INSTANCE = new MySingleton();&lt;br /&gt;&lt;br /&gt;    private MySingleton() {}&lt;br /&gt;&lt;br /&gt;    public static final MySingleton getInstance() {&lt;br /&gt;      return INSTANCE;&lt;br /&gt;    }&lt;br /&gt;	 &lt;br /&gt;	/**&lt;br /&gt;	* Normal deserialization returns a new instance of an object. This ensures that only one instance is in existence. &lt;br /&gt;         * Deserialization can either create a new instance and leave the deserialized object to be garbage collected or&lt;br /&gt;         * reuse the deserialized instance.&lt;br /&gt;	*/&lt;br /&gt;	private Object readResolve() throws ObjectStreamException {&lt;br /&gt;	  return INSTANCE;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 14 Feb 2007 02:42:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3519</guid>
      <author>wjchay (Chay Weei Jye)</author>
    </item>
    <item>
      <title>singleton in ruby</title>
      <link>http://snippets.dzone.com/posts/show/2794</link>
      <description>// simple singleton&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Singleton &lt;br /&gt;  private_class_method :new&lt;br /&gt;  @@singleton = nil&lt;br /&gt;  &lt;br /&gt;  def Singleton.create&lt;br /&gt;    @@singleton = new unless @@singleton&lt;br /&gt;    @@singleton&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 07 Oct 2006 14:47:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2794</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>Singleton pattern in python</title>
      <link>http://snippets.dzone.com/posts/show/735</link>
      <description>&lt;code&gt;&lt;br /&gt;class Borg:&lt;br /&gt;    __shared_state = {}&lt;br /&gt;    def __init__(self):&lt;br /&gt;        self.__dict__ = self.__shared_state&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;What a pythonic way to use Singleton. It uses shared-state&lt;br /&gt;approach where you can actually have many instances as you want&lt;br /&gt;but they all share the same state. See Alex's &lt;a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531&gt;recipe&lt;/a&gt;.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; b = Borg()&lt;br /&gt;&gt;&gt;&gt; b.x = 1&lt;br /&gt;&gt;&gt;&gt; c = Borg()&lt;br /&gt;&gt;&gt;&gt; c.x&lt;br /&gt;1&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 18 Sep 2005 13:01:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/735</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>python singleton and singleton borg</title>
      <link>http://snippets.dzone.com/posts/show/651</link>
      <description>Singleton with same states &lt;br /&gt;&lt;code&gt;&lt;br /&gt;# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531&lt;br /&gt;class Borg:&lt;br /&gt;    __shared_state = {}&lt;br /&gt;    def __init__(self):&lt;br /&gt;        self.__dict__ = self.__shared_state&lt;br /&gt;&lt;br /&gt;a=Borg()&lt;br /&gt;a.toto = 12&lt;br /&gt;&lt;br /&gt;b=Borg()&lt;br /&gt;print b.toto&lt;br /&gt;print id(a),id(b) # different ! but states are sames&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;real Singleton instance&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Singleton(object):&lt;br /&gt;    def __new__(type):&lt;br /&gt;        if not '_the_instance' in type.__dict__:&lt;br /&gt;            type._the_instance = object.__new__(type)&lt;br /&gt;        return type._the_instance&lt;br /&gt;&lt;br /&gt;a=Singleton()&lt;br /&gt;a.toto = 12&lt;br /&gt;&lt;br /&gt;b=Singleton()&lt;br /&gt;print b.toto&lt;br /&gt;print id(a),id(b) # the same !!&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 08 Sep 2005 00:12:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/651</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>Flexible Singleton</title>
      <link>http://snippets.dzone.com/posts/show/135</link>
      <description>PHP function Singleton allows you holding exactly one object of a class in memory. If an object of a given class doesn't exist, Singleton will create and store a new one in static array singleton. It then (and otherwise) will return a reference to it.&lt;br /&gt;&lt;br /&gt;Adding an optional ID allows holding more than one object a class. Assigned ID will distinguish array keys.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt; * Singleton Repository&lt;br /&gt; * @param string $class PHP Class Name&lt;br /&gt; * @param string $id Optional Object ID&lt;br /&gt; * @return reference Reference to existing Object&lt;br /&gt; */&lt;br /&gt;function &amp;Singleton($class, $id='') {&lt;br /&gt;  static $singleton = array();&lt;br /&gt;  if (!array_key_exists($class.$id, $singleton))&lt;br /&gt;    $singleton[$class.$id] = &amp;new $class();&lt;br /&gt;  $reference = &amp;$singleton[$class.$id];&lt;br /&gt;  return $reference;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Use like this:&lt;br /&gt;[edit] I'm sorry there was a mistake in the first exmaple for three days or so. Fixed.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# first call: create object&lt;br /&gt;$site_user=&amp;Singleton('Student');&lt;br /&gt;$site_user-&gt;Drink_Beer(5);&lt;br /&gt;&lt;br /&gt;# second call: get a reference&lt;br /&gt;$current_user=&amp;Singleton('Student');&lt;br /&gt;echo $current_user-&gt;Show_Beers_Counter();&lt;br /&gt;#will be 5&lt;br /&gt;&lt;br /&gt;#Two different objects&lt;br /&gt;$one=&amp;Singleton('Some_Class','one');&lt;br /&gt;$two=&amp;Singleton('Some_Class','two');&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Works fine with PHP4, not tested on PHP5</description>
      <pubDate>Mon, 11 Apr 2005 05:52:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/135</guid>
      <author>drwitt (Carsten Witt)</author>
    </item>
  </channel>
</rss>
