www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The singleton design pattern in D, C++ and Java

reply Justin Johansson <no spam.com> writes:
Which language out of C++, D and Java does this classical
"GoF" (gang-of-four***) design pattern best?

*** http://en.wikipedia.org/wiki/Design_Patterns

For my take, I prefer synchronization-free implementations.

Surely this topic has been around before???

<bystander awareness="clueless" name="Justin"/>
Jul 16 2010
next sibling parent reply BLS <windevguy hotmail.de> writes:
On 16/07/2010 15:24, Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?

 *** http://en.wikipedia.org/wiki/Design_Patterns

 For my take, I prefer synchronization-free implementations.

 Surely this topic has been around before???

 <bystander awareness="clueless" name="Justin"/>
Note sure about "Best" but I like this D implementation. auto st1 = Singleton(); auto st2 = Singleton(); final class Singleton { private static Singleton st; static this() { st = new Singleton; } static Singleton opCall() { return st; } //.... }
Jul 16 2010
next sibling parent reply Justin Johansson <no spam.com> writes:
BLS wrote:
 On 16/07/2010 15:24, Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?

 *** http://en.wikipedia.org/wiki/Design_Patterns

 For my take, I prefer synchronization-free implementations.

 Surely this topic has been around before???

 <bystander awareness="clueless" name="Justin"/>
Note sure about "Best" but I like this D implementation. auto st1 = Singleton(); auto st2 = Singleton(); final class Singleton { private static Singleton st; static this() { st = new Singleton; } static Singleton opCall() { return st; } //.... }
Your judicial use of "final" in the class declspec is really *cool*. Were I funded like a Facebook enterprise, I would be sure to offer you secure employment together with a share offering. :-) Now how do C++ and Java people do it? Cheers, JJ
Jul 16 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 16 Jul 2010 09:52:26 -0400, Justin Johansson <no spam.com> wrote:

 BLS wrote:
 On 16/07/2010 15:24, Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?

 *** http://en.wikipedia.org/wiki/Design_Patterns

 For my take, I prefer synchronization-free implementations.

 Surely this topic has been around before???

 <bystander awareness="clueless" name="Justin"/>
Note sure about "Best" but I like this D implementation. auto st1 = Singleton(); auto st2 = Singleton(); final class Singleton { private static Singleton st; static this() { st = new Singleton; } static Singleton opCall() { return st; } //.... }
Your judicial use of "final" in the class declspec is really *cool*. Were I funded like a Facebook enterprise, I would be sure to offer you secure employment together with a share offering. :-) Now how do C++ and Java people do it?
wikipedia to the rescue :) http://en.wikipedia.org/wiki/Singleton_pattern I see you reference wikipedia in the original post, so I'm surprised you didn't see this... Note that BLS' solution is not lazy, so it doesn't really make it more elegant than the non-lazy Java solution: class Singleton { private Singleton _instance = new Singleton(); public Singleton getInstance() { return _instance; } private Singleton(); // disable public constructor } -Steve
Jul 16 2010
prev sibling parent "Nick Sabalausky" <a a.a> writes:
"BLS" <windevguy hotmail.de> wrote in message 
news:i1pnfn$166f$1 digitalmars.com...
 On 16/07/2010 15:24, Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?

 *** http://en.wikipedia.org/wiki/Design_Patterns

 For my take, I prefer synchronization-free implementations.

 Surely this topic has been around before???

 <bystander awareness="clueless" name="Justin"/>
Note sure about "Best" but I like this D implementation. auto st1 = Singleton(); auto st2 = Singleton(); final class Singleton { private static Singleton st; static this() { st = new Singleton; } static Singleton opCall() { return st; } //.... }
Whenever possible, I like to just make a static class. The limitation is that you can't pass it around, at least not in D, and IIRC there may be some problems having it participate in inheritance, but a lot of times that doesn't matter.
Jul 16 2010
prev sibling next sibling parent reply dsimcha <dsimcha yahoo.com> writes:
== Quote from Justin Johansson (no spam.com)'s article
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
 *** http://en.wikipedia.org/wiki/Design_Patterns
 For my take, I prefer synchronization-free implementations.
 Surely this topic has been around before???
 <bystander awareness="clueless" name="Justin"/>
D makes it very easy to create an efficient thread-safe Singleton implementation, using a method that was inspired by broken double-checked locking, but uses thread-local storage to be correct. The following algorithm guarantees that the synchronized block guarding the Singleton will be entered at most once per thread throughout the lifetime of the program. I invented it myself, but it's fairly simple and might have been independently invented elsewhere: class Singleton { private: static bool initialized; // Thread-local __gshared static Singleton instance; this() {} public: static Singleton getInstance() { if(initialized) { return instance; } synchronized(Singleton.classinfo) { scope(success) initialized = true; if(instance !is null) { return instance; } instance = new Singleton; return instance; } } }
Jul 16 2010
next sibling parent Max Samukha <spam spam.com> writes:
On 16.07.2010 16:49, dsimcha wrote:

 D makes it very easy to create an efficient thread-safe Singleton
implementation,
 using a method that was inspired by broken double-checked locking, but uses
 thread-local storage to be correct.  The following algorithm guarantees that
the
 synchronized block guarding the Singleton will be entered at most once per
thread
 throughout the lifetime of the program.  I invented it myself, but it's fairly
 simple and might have been independently invented elsewhere:

 class Singleton {
 private:
      static bool initialized;  // Thread-local
      __gshared static Singleton instance;

      this() {}

 public:

      static Singleton getInstance() {
          if(initialized) {
              return instance;
          }

          synchronized(Singleton.classinfo) {
              scope(success) initialized = true;
              if(instance !is null) {
                  return instance;
              }

              instance = new Singleton;
              return instance;
         }
      }
 }
I invented it here: http://www.digitalmars.com/d/archives/digitalmars/D/Static_constructors_in_circularly_imported_modules_-_again_110518.html#N110527 Which proves we are great minds :)
Jul 16 2010
prev sibling parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
On 16/07/2010 14:49, dsimcha wrote:
 == Quote from Justin Johansson (no spam.com)'s article
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
 *** http://en.wikipedia.org/wiki/Design_Patterns
 For my take, I prefer synchronization-free implementations.
 Surely this topic has been around before???
 <bystander awareness="clueless" name="Justin"/>
D makes it very easy to create an efficient thread-safe Singleton implementation, using a method that was inspired by broken double-checked locking, but uses thread-local storage to be correct. The following algorithm guarantees that the synchronized block guarding the Singleton will be entered at most once per thread throughout the lifetime of the program. I invented it myself, but it's fairly simple and might have been independently invented elsewhere: class Singleton { private: static bool initialized; // Thread-local __gshared static Singleton instance; this() {} public: static Singleton getInstance() { if(initialized) { return instance; } synchronized(Singleton.classinfo) { scope(success) initialized = true; if(instance !is null) { return instance; } instance = new Singleton; return instance; } } }
And whats wrong with using the normal Double-Checked Locking idiom? Something like this: class Singleton { private: static shared Singleton instance; this() {} public: static shared(Singleton) getInstance() { if(instance !is null) { return instance; } synchronized(Singleton.classinfo) { instance = new shared(Singleton)(); return instance; } } } } Doesnt D2's memory model for shared data guarantee the required data consistency for this code? (ie, that other threads only see the 'instance' variable being written after all of Singleton's construction is finished, ie, all writes are externally visible) ? -- Bruno Medeiros - Software Engineer
Jul 26 2010
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
Are we still talking singleton? I thought that it is considered an anti-pattern already. :) As a teaser: What problem does it solve that can't be solved by creating just one object of a type? Ali "Forgive Me Father, for I Have Singleton’ed" - Kevlin Henney
Jul 16 2010
next sibling parent dsimcha <dsimcha yahoo.com> writes:
== Quote from Ali Çehreli (acehreli yahoo.com)'s article
 Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
Are we still talking singleton? I thought that it is considered an anti-pattern already. :) As a teaser: What problem does it solve that can't be solved by creating just one object of a type? Ali "Forgive Me Father, for I Have Singleton’ed" - Kevlin Henney
Creating that one object lazily, tracking whether you've created one already, and providing an access point for that object. Frankly, I don't see what's so bad about global variables and singletons per se. I definitely agree with the consensus that the kind of action at a distance that they cause if used to share frequently mutated state between distant parts of a program is evil. However, there are a decent number of use cases that don't lead to this: 1. Storing program parameters that are set at start up from the command line, a configuration file, etc. and are then treated as constants for the duration of the "meat" of the program. IMHO this is more like having a global constant, even if it's only const by convention and not enforced as such by the compiler. 2. As a performance optimization, they're useful for recycling scratch space or objects that are expensive to create (though usually you'd want a thread-local Singleton in these cases, or possibly a function-local static variable).
Jul 16 2010
prev sibling next sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Ali Çehreli" <acehreli yahoo.com> wrote in message 
news:i1q4bk$21jp$1 digitalmars.com...
 Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
Are we still talking singleton? I thought that it is considered an anti-pattern already. :) As a teaser: What problem does it solve that can't be solved by creating just one object of a type?
In languages that don't allow functions outside a class, they're useful as a make-shift substitute for a module with free-floating functions. Or at least if you count static classes as a form of singleton, anyway.
Jul 16 2010
parent reply Jonathan M Davis <jmdavisprog gmail.com> writes:
On Friday, July 16, 2010 11:21:28 Nick Sabalausky wrote:
 "Ali =C7ehreli" <acehreli yahoo.com> wrote in message
 news:i1q4bk$21jp$1 digitalmars.com...
=20
 Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
=20 Are we still talking singleton? I thought that it is considered an anti-pattern already. :) =20 As a teaser: What problem does it solve that can't be solved by creating just one object of a type?
=20 In languages that don't allow functions outside a class, they're useful as a make-shift substitute for a module with free-floating functions. Or at least if you count static classes as a form of singleton, anyway.
I actually would have considered that to be more of a "nullington," since = the=20 class can't instatiated at all. All it really is is a way to namespace the= =20 functions, since functionally-speaking, the class only serves as a namespac= e=20 rather than a real class. Personally, I do use singleton from time-to-time and have found it quite us= eful.=20 I wasn't aware that anyone thought that it was a bad idea. =2D Jonathan M Davis
Jul 16 2010
parent reply "Nick Sabalausky" <a a.a> writes:
"Jonathan M Davis" <jmdavisprog gmail.com> wrote in message 
news:mailman.366.1279305632.24349.digitalmars-d puremagic.com...
Personally, I do use singleton from time-to-time and have found it quite 
useful.
I wasn't aware that anyone thought that it was a bad idea.
That was news to me too. My guess is that maybe there were a lot of people using it for things that may have *seemed* appropriate for a singleton but really weren't, like abstractions for the keyboard, mouse or screen/monitor.
Jul 16 2010
parent Lutger <lutger.blijdestijn gmail.com> writes:
Nick Sabalausky wrote:

 "Jonathan M Davis" <jmdavisprog gmail.com> wrote in message
 news:mailman.366.1279305632.24349.digitalmars-d puremagic.com...
Personally, I do use singleton from time-to-time and have found it quite
useful.
I wasn't aware that anyone thought that it was a bad idea.
That was news to me too. My guess is that maybe there were a lot of people using it for things that may have *seemed* appropriate for a singleton but really weren't, like abstractions for the keyboard, mouse or screen/monitor.
More than that, singletons are often used as an 'object oriented' way of creating global variables, hence the bad reputation. http://www.c2.com/cgi-bin/wiki?SingletonsAreEvil
Jul 26 2010
prev sibling parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
On 16/07/2010 18:18, Ali Çehreli wrote:
 Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
Are we still talking singleton? I thought that it is considered an anti-pattern already. :)
First time I've heard this as well. I searched the web, and woah, there does seem to be quite a few people who think the same, but frankly, upon reading their arguments against singleton (in the sense that singleton should not be used), most of them don't add up. In fact some of those arguments are quite idiotic, a lot of people are totally misunderstanding singleton. (im not going to botter arguing why, and I hope people here don't think singleton is inherently bad) Cause man, I haven't seen such idiocy since when I joined a new company and found the then lead developer enforced a policy in our Java codebase of single return statements. Because multiple return statements were very bad and should *only* be used if performance necessitated that. Lolz. Talk about software engineering best practices from the 60's. -- Bruno Medeiros - Software Engineer
Aug 03 2010
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Bruno Medeiros wrote:
 On 16/07/2010 18:18, Ali Çehreli wrote:
 Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
Are we still talking singleton? I thought that it is considered an anti-pattern already. :)
First time I've heard this as well. I searched the web, and woah, there does seem to be quite a few people who think the same, but frankly, upon reading their arguments against singleton (in the sense that singleton should not be used), most of them don't add up. In fact some of those arguments are quite idiotic, a lot of people are totally misunderstanding singleton. (im not going to botter arguing why, and I hope people here don't think singleton is inherently bad) Cause man, I haven't seen such idiocy since when I joined a new company and found the then lead developer enforced a policy in our Java codebase of single return statements. Because multiple return statements were very bad and should *only* be used if performance necessitated that. Lolz. Talk about software engineering best practices from the 60's.
OUCHHHHHHHH... Andrei
Aug 03 2010
parent reply "Mike James" <foo bar.com> writes:
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message 
news:i39d0s$2596$1 digitalmars.com...
 Bruno Medeiros wrote:
 On 16/07/2010 18:18, Ali Çehreli wrote:
 Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
Are we still talking singleton? I thought that it is considered an anti-pattern already. :)
First time I've heard this as well. I searched the web, and woah, there does seem to be quite a few people who think the same, but frankly, upon reading their arguments against singleton (in the sense that singleton should not be used), most of them don't add up. In fact some of those arguments are quite idiotic, a lot of people are totally misunderstanding singleton. (im not going to botter arguing why, and I hope people here don't think singleton is inherently bad) Cause man, I haven't seen such idiocy since when I joined a new company and found the then lead developer enforced a policy in our Java codebase of single return statements. Because multiple return statements were very bad and should *only* be used if performance necessitated that. Lolz. Talk about software engineering best practices from the 60's.
OUCHHHHHHHH... Andrei
Having a single point of exit from a function is still a design practice in the Joint Strike Fighter (JSF) C++ Coding Standard (Rule 113) and is a requirement in MISRA C++:2008 (Rule 6-6-5) and MISRA C:2004 (Rule 14.4) and also a requirement to writing software under IEC61508 (Part 3, Table B.9). So its still an engineering best practice in the 10's :-) -=mike=-
Aug 05 2010
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Mike James wrote:
 "Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message 
 news:i39d0s$2596$1 digitalmars.com...
 Bruno Medeiros wrote:
 On 16/07/2010 18:18, Ali Çehreli wrote:
 Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
Are we still talking singleton? I thought that it is considered an anti-pattern already. :)
First time I've heard this as well. I searched the web, and woah, there does seem to be quite a few people who think the same, but frankly, upon reading their arguments against singleton (in the sense that singleton should not be used), most of them don't add up. In fact some of those arguments are quite idiotic, a lot of people are totally misunderstanding singleton. (im not going to botter arguing why, and I hope people here don't think singleton is inherently bad) Cause man, I haven't seen such idiocy since when I joined a new company and found the then lead developer enforced a policy in our Java codebase of single return statements. Because multiple return statements were very bad and should *only* be used if performance necessitated that. Lolz. Talk about software engineering best practices from the 60's.
OUCHHHHHHHH... Andrei
Having a single point of exit from a function is still a design practice in the Joint Strike Fighter (JSF) C++ Coding Standard (Rule 113) and is a requirement in MISRA C++:2008 (Rule 6-6-5) and MISRA C:2004 (Rule 14.4) and also a requirement to writing software under IEC61508 (Part 3, Table B.9). So its still an engineering best practice in the 10's :-)
I know! And that doesn't help either! With exceptions, SESE effectively becomes a dangerous illusion. Andrei
Aug 05 2010
parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
On 05/08/2010 17:18, Andrei Alexandrescu wrote:
 Mike James wrote:
 "Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message
 news:i39d0s$2596$1 digitalmars.com...
 Bruno Medeiros wrote:
 On 16/07/2010 18:18, Ali Çehreli wrote:
 Justin Johansson wrote:
 Which language out of C++, D and Java does this classical
 "GoF" (gang-of-four***) design pattern best?
Are we still talking singleton? I thought that it is considered an anti-pattern already. :)
First time I've heard this as well. I searched the web, and woah, there does seem to be quite a few people who think the same, but frankly, upon reading their arguments against singleton (in the sense that singleton should not be used), most of them don't add up. In fact some of those arguments are quite idiotic, a lot of people are totally misunderstanding singleton. (im not going to botter arguing why, and I hope people here don't think singleton is inherently bad) Cause man, I haven't seen such idiocy since when I joined a new company and found the then lead developer enforced a policy in our Java codebase of single return statements. Because multiple return statements were very bad and should *only* be used if performance necessitated that. Lolz. Talk about software engineering best practices from the 60's.
OUCHHHHHHHH... Andrei
Having a single point of exit from a function is still a design practice in the Joint Strike Fighter (JSF) C++ Coding Standard (Rule 113) and is a requirement in MISRA C++:2008 (Rule 6-6-5) and MISRA C:2004 (Rule 14.4) and also a requirement to writing software under IEC61508 (Part 3, Table B.9). So its still an engineering best practice in the 10's :-)
I know! And that doesn't help either! With exceptions, SESE effectively becomes a dangerous illusion. Andrei
My thoughts exactly. Although its not the only reason for it being outdated. Going back to the previous post, well, yes, if you are coding in C or some similar pre-OO, strutured programming language (in other words, languages from the 60s and 70s :p) it still makes *some* sense to follow that rule. The context and principles for the rule are still valid. Perhaps also for C++ if you restrict yourself to just a subset of C++ functionality. Like in the case of the JSF C++ coding standard, which also says: "C++ exceptions shall not be used (i.e. throw, catch and try shall not be used.)" ! But for most, if not all, newer-than-c mainstream languages, its just a completely bonkers idea, and not just because of exceptions. Anyways, moving on... -- Bruno Medeiros - Software Engineer
Aug 06 2010