digitalmars.D - The singleton design pattern in D, C++ and Java
- Justin Johansson (6/6) Jul 16 2010 Which language out of C++, D and Java does this classical
- BLS (15/21) Jul 16 2010 Note sure about "Best" but I like this D implementation.
- Justin Johansson (6/35) Jul 16 2010 Your judicial use of "final" in the class declspec is really *cool*.
- Steven Schveighoffer (16/45) Jul 16 2010 wikipedia to the rescue :)
- Nick Sabalausky (6/30) Jul 16 2010 Whenever possible, I like to just make a static class. The limitation is...
- dsimcha (27/33) Jul 16 2010 D makes it very easy to create an efficient thread-safe Singleton implem...
- Max Samukha (4/30) Jul 16 2010 I invented it here:
- Bruno Medeiros (25/58) Jul 26 2010 And whats wrong with using the normal Double-Checked Locking idiom?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/9) Jul 16 2010 Are we still talking singleton? I thought that it is considered an
- dsimcha (15/24) Jul 16 2010 Creating that one object lazily, tracking whether you've created one alr...
- Nick Sabalausky (5/12) Jul 16 2010 In languages that don't allow functions outside a class, they're useful ...
- Jonathan M Davis (12/28) Jul 16 2010 I actually would have considered that to be more of a "nullington," sinc...
- Nick Sabalausky (5/8) Jul 16 2010 That was news to me too. My guess is that maybe there were a lot of peop...
- Lutger (4/15) Jul 26 2010 More than that, singletons are often used as an 'object oriented' way of...
- Bruno Medeiros (16/21) Aug 03 2010 First time I've heard this as well. I searched the web, and woah, there
- Andrei Alexandrescu (3/25) Aug 03 2010 OUCHHHHHHHH...
- Mike James (8/33) Aug 05 2010 Having a single point of exit from a function is still a design practice...
- Andrei Alexandrescu (4/40) Aug 05 2010 I know! And that doesn't help either!
- Bruno Medeiros (16/57) Aug 06 2010 My thoughts exactly. Although its not the only reason for it being outda...
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
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
BLS wrote:On 16/07/2010 15:24, Justin Johansson wrote: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, JJWhich 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
On Fri, 16 Jul 2010 09:52:26 -0400, Justin Johansson <no spam.com> wrote:BLS wrote: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 } -SteveOn 16/07/2010 15:24, Justin Johansson wrote: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?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
"BLS" <windevguy hotmail.de> wrote in message news:i1pnfn$166f$1 digitalmars.com...On 16/07/2010 15:24, Justin Johansson wrote: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.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
== Quote from Justin Johansson (no spam.com)'s articleWhich 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
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
On 16/07/2010 14:49, dsimcha wrote:== Quote from Justin Johansson (no spam.com)'s articleAnd 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 EngineerWhich 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 26 2010
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
== Quote from Ali Çehreli (acehreli yahoo.com)'s articleJustin Johansson wrote: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).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
"Ali Çehreli" <acehreli yahoo.com> wrote in message news:i1q4bk$21jp$1 digitalmars.com...Justin Johansson wrote: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.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?
Jul 16 2010
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... =20I 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 DavisJustin Johansson wrote:=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.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?
Jul 16 2010
"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
Nick Sabalausky wrote:"Jonathan M Davis" <jmdavisprog gmail.com> wrote in message news:mailman.366.1279305632.24349.digitalmars-d puremagic.com...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?SingletonsAreEvilPersonally, 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 26 2010
On 16/07/2010 18:18, Ali Çehreli wrote:Justin Johansson wrote: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 EngineerWhich 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. :)
Aug 03 2010
Bruno Medeiros wrote:On 16/07/2010 18:18, Ali Çehreli wrote:OUCHHHHHHHH... AndreiJustin Johansson wrote: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.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. :)
Aug 03 2010
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message news:i39d0s$2596$1 digitalmars.com...Bruno Medeiros wrote: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=-On 16/07/2010 18:18, Ali Çehreli wrote:OUCHHHHHHHH... AndreiJustin Johansson wrote: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.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. :)
Aug 05 2010
Mike James wrote:"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message news:i39d0s$2596$1 digitalmars.com...I know! And that doesn't help either! With exceptions, SESE effectively becomes a dangerous illusion. AndreiBruno Medeiros wrote: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 :-)On 16/07/2010 18:18, Ali Çehreli wrote:OUCHHHHHHHH... AndreiJustin Johansson wrote: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.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. :)
Aug 05 2010
On 05/08/2010 17:18, Andrei Alexandrescu wrote:Mike James wrote: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"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message news:i39d0s$2596$1 digitalmars.com...I know! And that doesn't help either! With exceptions, SESE effectively becomes a dangerous illusion. AndreiBruno Medeiros wrote: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 :-)On 16/07/2010 18:18, Ali Çehreli wrote:OUCHHHHHHHH... AndreiJustin Johansson wrote: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.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. :)
Aug 06 2010