www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is this a good singleton?

reply Vladde Nordholm <v vladde.net> writes:
Hello. I have this singleton,

------------------------------------------------------
class Singleton
{
     private this() {}
     static __gshared typeof(this) instance = new this;
}
------------------------------------------------------

and I wonder if it has any weaknesses. Or is there a better way 
to make a Singleton?

,vladde,
Feb 13 2016
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/13/2016 10:58 AM, Vladde Nordholm wrote:
 Hello. I have this singleton,

 ------------------------------------------------------
 class Singleton
 {
      private this() {}
      static __gshared typeof(this) instance = new this;
 }
 ------------------------------------------------------

 and I wonder if it has any weaknesses. Or is there a better way to make
 a Singleton?

 ,vladde,
David Simcha's DConf 2013 presentation has a singleton implementation at 27:55: https://www.youtube.com/watch?v=yMNMV9JlkcQ Ali
Feb 13 2016
parent Charles <csmith.ku2013 gmail.com> writes:
On Saturday, 13 February 2016 at 19:32:33 UTC, Ali Çehreli wrote:
 David Simcha's DConf 2013 presentation has a singleton 
 implementation at 27:55:

   https://www.youtube.com/watch?v=yMNMV9JlkcQ

 Ali
Neat video! Watched the singleton section to end up watching the rest of the video. Anything every come of the std.patterns idea?
Feb 13 2016
prev sibling parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2016-02-13 at 18:58 +0000, Vladde Nordholm via Digitalmars-d-
learn wrote:
 Hello. I have this singleton,
=20
 ------------------------------------------------------
 class Singleton
 {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0private this() {}
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0static __gshared typeof(this) instance =3D =
new this;
 }
 ------------------------------------------------------
=20
 and I wonder if it has any weaknesses. Or is there a better way=C2=A0
 to make a Singleton?
Following the ACCU consensus: there is never, ever a good Singleton or reason to contemplate using one. Obviously though there are some good ones: about dialogues in GUIs for example. The good/evil-ness of Singleton is definitely in it's use: most people use Singleton wrongly, most uses should be banned. Hence the ACCU consensus. It's all about coupling and mostly testability of code.. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Feb 13 2016
parent Vladde Nordholm <v vladde.net> writes:
On Sunday, 14 February 2016 at 07:16:54 UTC, Russel Winder wrote:
 On Sat, 2016-02-13 at 18:58 +0000, Vladde Nordholm via 
 Digitalmars-d- learn wrote:
 [...]
Following the ACCU consensus: there is never, ever a good Singleton or reason to contemplate using one. Obviously though there are some good ones: about dialogues in GUIs for example. The good/evil-ness of Singleton is definitely in it's use: most people use Singleton wrongly, most uses should be banned. Hence the ACCU consensus. It's all about coupling and mostly testability of code..
Thanks. I'm using David Simcha's singleton now.
Feb 14 2016