www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Singleton in Action?

reply Ron Tarrant <rontarrant gmail.com> writes:
Hi guys,

I ran into another snag this morning while trying to implement a 
singleton. I found all kinds of examples of singleton 
definitions, but nothing about how to put them into practice.

Can someone show me a code example for how one would actually use 
a singleton pattern in D? When I did the same thing in PHP, it 
took me forever to wrap my brain around it, so I'm hoping to get 
there a little faster this time.

Here's the singleton code I've been playing with:

class DSingleton
{
	private:
	// Cache instantiation flag in thread-local bool
	// Thread local
	static bool instantiated_;

	// Thread global
	__gshared DSingleton instance_;

	this()
	{
		
	} // this()

	public:
	
	static DSingleton get()
	{
		if(!instantiated_)
		{
			synchronized(DSingleton.classinfo)
			{
				if(!instance_)
				{
					instance_ = new DSingleton();
					writeln("creating");
				}

				instantiated_ = true;
			}
		}
		else
		{
			writeln("not created");
		}

		return(instance_);
		
	} // DSingleton()

} // class DSingleton

So, my big question is, do I instantiate like this:

DSingleton singleton = new DSingleton;

Or like this:

DSingleton singleton = singleton.get();

And subsequent calls would be...? The same? Using get() only?
Feb 02
next sibling parent Russel Winder <russel winder.org.uk> writes:
On Sat, 2019-02-02 at 16:56 +0000, Ron Tarrant via Digitalmars-d-learn wrot=
e:
 Hi guys,
and gals.
 I ran into another snag this morning while trying to implement a=20
 singleton. I found all kinds of examples of singleton=20
 definitions, but nothing about how to put them into practice.
General, these days, as far as I know, Singleton is considered one of the ultimate anti-patterns of software development. There are many positive reasons for this very negative view of Singleton especially if there is any hint of concurrency or parallelism involved. Also Singletons tend to deny sensible testing.
 Can someone show me a code example for how one would actually use=20
 a singleton pattern in D? When I did the same thing in PHP, it=20
 took me forever to wrap my brain around it, so I'm hoping to get=20
 there a little faster this time.
I'd suggest just not going here. Let's not have Singleton in D code. Alternatives include "Parameterize From Above" which I believe Kevlin Henne= y pushes strongly, and indeed he may be the source of the "pattern". (I belie= ve it is a design not a pattern using the proper definition of pattern.) If the resource really is "singleton" then creating an actor or agent is no= w the usual solution in languages such as D, Rust, Go, even C++.
=20
[=E2=80=A6]
 So, my big question is, do I instantiate like this:
=20
 DSingleton singleton =3D new DSingleton;
This seems wrong.
 Or like this:
=20
 DSingleton singleton =3D singleton.get();
getting seems like the one true way, if you really have to do this. Can I indent that Singleton not be part of the canon of D programming.
 And subsequent calls would be...? The same? Using get() only?
=20
=20
--=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 Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
Feb 02
prev sibling next sibling parent reply Eugene Wissner <belka caraus.de> writes:
On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:
 Hi guys,

 I ran into another snag this morning while trying to implement 
 a singleton. I found all kinds of examples of singleton 
 definitions, but nothing about how to put them into practice.

 Can someone show me a code example for how one would actually 
 use a singleton pattern in D? When I did the same thing in PHP, 
 it took me forever to wrap my brain around it, so I'm hoping to 
 get there a little faster this time.

 Here's the singleton code I've been playing with:

 class DSingleton
 {
 	private:
 	// Cache instantiation flag in thread-local bool
 	// Thread local
 	static bool instantiated_;

 	// Thread global
 	__gshared DSingleton instance_;

 	this()
 	{
 		
 	} // this()

 	public:
 	
 	static DSingleton get()
 	{
 		if(!instantiated_)
 		{
 			synchronized(DSingleton.classinfo)
 			{
 				if(!instance_)
 				{
 					instance_ = new DSingleton();
 					writeln("creating");
 				}

 				instantiated_ = true;
 			}
 		}
 		else
 		{
 			writeln("not created");
 		}

 		return(instance_);
 		
 	} // DSingleton()

 } // class DSingleton

 So, my big question is, do I instantiate like this:

 DSingleton singleton = new DSingleton;

 Or like this:

 DSingleton singleton = singleton.get();

 And subsequent calls would be...? The same? Using get() only?
Imho it looks fine. For creation get() should be always used, since it is the most convenient way to ensure that there is really only one instance of the singleton. Just make this() private, so only you can create new instances: private this() { } And you probably don't need instantiated_, you can always check whether instance_ is null or not. So: if (instance_ is null) { ... } else { ... }
Feb 02
parent reply Neia Neutuladh <neia ikeran.org> writes:
On Sat, 02 Feb 2019 17:34:11 +0000, Eugene Wissner wrote:
 For creation get() should be always used, since it is the most
 convenient way to ensure that there is really only one instance of the
 singleton. Just make this() private, so only you can create new
 instances:
 
 private this()
 {
 }
And consider putting the class in its own source file.
Feb 02
parent reply Ron Tarrant <rontarrant gmail.com> writes:
On Saturday, 2 February 2019 at 20:30:15 UTC, Neia Neutuladh 
wrote:

 And consider putting the class in its own source file.
Yes, by all means. Speaking of which... Considering the nature of a singleton such the one in the top post, I can't see it being possible to use one as a base class from which to derive other classes... speaking from a theoretical POV. Thanks, guys, for all the input.
Feb 03
parent reply Alex <sascha.orlov gmail.com> writes:
On Sunday, 3 February 2019 at 09:46:20 UTC, Ron Tarrant wrote:
 On Saturday, 2 February 2019 at 20:30:15 UTC, Neia Neutuladh 
 wrote:

 And consider putting the class in its own source file.
Yes, by all means. Speaking of which... Considering the nature of a singleton such the one in the top post, I can't see it being possible to use one as a base class from which to derive other classes... speaking from a theoretical POV. Thanks, guys, for all the input.
Isn't deriving a singleton even eviler as having one? ;) https://codeblog.jonskeet.uk/2006/01/19/singleton-inheritance/
Feb 03
parent reply Ron Tarrant <rontarrant gmail.com> writes:
On Sunday, 3 February 2019 at 10:28:51 UTC, Alex wrote:

 Isn't deriving a singleton even eviler as having one? ;)
Perhaps this was meant as rhetoric, but I think you may be right. This morning I was Googling "singleton replacement" and someone on another forum said Factory would do the job. Anyone have thoughts on that? (Personally, I don't see it, but I'm willing to update my position based on new evidence.)
Feb 03
next sibling parent reply Russel Winder <russel winder.org.uk> writes:
On Sun, 2019-02-03 at 14:42 +0000, Ron Tarrant via Digitalmars-d-learn wrot=
e:
 On Sunday, 3 February 2019 at 10:28:51 UTC, Alex wrote:
=20
 Isn't deriving a singleton even eviler as having one? ;)
=20 Perhaps this was meant as rhetoric, but I think you may be right. =20 This morning I was Googling "singleton replacement" and someone=20 on another forum said Factory would do the job. Anyone have=20 thoughts on that? =20 (Personally, I don't see it, but I'm willing to update my=20 position based on new evidence.)
There is a lot of good stuff (both positive and negative) on Singleton here= , but there is also a bit of prejudice and bigotry. Many of the links are wor= th looking through. https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons The good use case for Singleton is very rare, most people use them wrongly.= It is all about eschewing all global state except when it is the one and only = way of doing the design correctly. But then you have to use it correctly. I currently have two Singletons in all my code, one I am trying to get rid = of, the other is fair enough. I think, but I'd still like to get rid of it. --=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 Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
Feb 03
parent Ron Tarrant <rontarrant gmail.com> writes:
On Sunday, 3 February 2019 at 15:33:15 UTC, Russel Winder wrote:

 There is a lot of good stuff (both positive and negative) on 
 Singleton here, but there is also a bit of prejudice and 
 bigotry. Many of the links are worth looking through.

 https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons
Thanks, Russell. I'll take a look.
 The good use case for Singleton is very rare, most people use 
 them wrongly. It is all about eschewing all global state except 
 when it is the one and only way of doing the design correctly. 
 But then you have to use it correctly.
Yup. I've gone back and forth on usage, too. The cases I found for a Singleton ten years ago may have better solutions today. Like a command dispatcher, a configuration changes tracker, a keyboard pre-processor, an undo/redo stack, and the application itself.
 I currently have two Singletons in all my code, one I am trying 
 to get rid of, the other is fair enough. I think, but I'd still 
 like to get rid of it.
I'd be curious to hear what you have/will replace a singleton with.
Feb 04
prev sibling parent Kagamin <spam here.lot> writes:
On Sunday, 3 February 2019 at 14:42:13 UTC, Ron Tarrant wrote:
 This morning I was Googling "singleton replacement" and someone 
 on another forum said Factory would do the job. Anyone have 
 thoughts on that?
It's usually replaced with inversion of control: the service instance is passed as an argument to object constructors, a related pattern is service provider when you need to access many services. Though it needs a more involved code organization compared to singleton. As for testing, singletons can be tested if you provide a setter method that allows to set a custom singleton instance.
Feb 04
prev sibling next sibling parent reply Ron Tarrant <rontarrant gmail.com> writes:
Thanks for the replies, fellow programmers. (generic, unisex, PC, 
and all-encompassing)

If I could trouble someone for a complete working example so I 
have something to study, that would be excellent.
Feb 02
next sibling parent reply Andre Pany <andre s-e-a-p.de> writes:
On Saturday, 2 February 2019 at 19:23:58 UTC, Ron Tarrant wrote:
 Thanks for the replies, fellow programmers. (generic, unisex, 
 PC, and all-encompassing)

 If I could trouble someone for a complete working example so I 
 have something to study, that would be excellent.
I found here an example: https://rosettacode.org/wiki/Singleton#D Kind regards Andre
Feb 02
next sibling parent Ron Tarrant <rontarrant gmail.com> writes:
On Saturday, 2 February 2019 at 19:40:25 UTC, Andre Pany wrote:

 I found here an example:
 https://rosettacode.org/wiki/Singleton#D

 Kind regards
 Andre
Thanks, Andre. Exactly what I was hoping for.
Feb 03
prev sibling parent reply Ron Tarrant <rontarrant gmail.com> writes:
On Saturday, 2 February 2019 at 19:40:25 UTC, Andre Pany wrote:

 https://rosettacode.org/wiki/Singleton#D
Do you know if this is for a current version of D? The compiler is choking on the import statements, complaining that it can't read std/thread.d and std/c/time.d
Feb 03
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Sunday, February 3, 2019 2:41:48 AM MST Ron Tarrant via Digitalmars-d-
learn wrote:
 On Saturday, 2 February 2019 at 19:40:25 UTC, Andre Pany wrote:
 https://rosettacode.org/wiki/Singleton#D
Do you know if this is for a current version of D? The compiler is choking on the import statements, complaining that it can't read std/thread.d and std/c/time.d
I don't recall std.thread ever existing, and std.c.time hasn't been around for a while. Thread is in core.thread, and all of the C bindings for standard C and OS APIs are supposed to be in druntime. So, the equivalent to C's time.h would be core.stdc.time. - Jonathan M Davis
Feb 03
parent Ron Tarrant <rontarrant gmail.com> writes:
On Sunday, 3 February 2019 at 11:17:38 UTC, Jonathan M Davis 
wrote:

 I don't recall std.thread ever existing, and std.c.time hasn't 
 been around for a while. Thread is in core.thread, and all of 
 the C bindings for standard C and OS APIs are supposed to be in 
 druntime. So, the equivalent to C's time.h would be 
 core.stdc.time.

 - Jonathan M Davis
Thanks for clearing that up, Jonathan.
Feb 03
prev sibling parent angel <andrey.gelman gmail.com> writes:
On Saturday, 2 February 2019 at 19:23:58 UTC, Ron Tarrant wrote:
 Thanks for the replies, fellow programmers. (generic, unisex, 
 PC, and all-encompassing)

 If I could trouble someone for a complete working example so I 
 have something to study, that would be excellent.
I think that's what you really need: [1] https://davesdprogramming.wordpress.com/ [2] https://www.youtube.com/watch?v=ZHmIAdlNtiM
Feb 02
prev sibling next sibling parent Norm <norm.rowtree gmail.com> writes:
On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:
 Hi guys,

 I ran into another snag this morning while trying to implement 
 a singleton. I found all kinds of examples of singleton 
 definitions, but nothing about how to put them into practice.

 [...]
If you haven't already been to the d-idioms site it is well worth a look: https://p0nce.github.io/d-idioms/ It has a singleton example that you may find useful. https://p0nce.github.io/d-idioms/#Leveraging-TLS-for-a-fast-thread-safe-singleton bye, norm
Feb 02
prev sibling next sibling parent Norm <norm.rowtree gmail.com> writes:
On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:
 Hi guys,

 I ran into another snag this morning while trying to implement 
 a singleton. I found all kinds of examples of singleton 
 definitions, but nothing about how to put them into practice.

 Can someone show me a code example for how one would actually 
 use a singleton pattern in D? When I did the same thing in PHP, 
 it took me forever to wrap my brain around it, so I'm hoping to 
 get there a little faster this time.

 Here's the singleton code I've been playing with:

 class DSingleton
 {
 	private:
 	// Cache instantiation flag in thread-local bool
 	// Thread local
 	static bool instantiated_;

 	// Thread global
 	__gshared DSingleton instance_;

 	this()
 	{
 		
 	} // this()

 	public:
 	
 	static DSingleton get()
 	{
 		if(!instantiated_)
 		{
 			synchronized(DSingleton.classinfo)
 			{
 				if(!instance_)
 				{
 					instance_ = new DSingleton();
 					writeln("creating");
 				}

 				instantiated_ = true;
 			}
 		}
 		else
 		{
 			writeln("not created");
 		}

 		return(instance_);
 		
 	} // DSingleton()

 } // class DSingleton

 So, my big question is, do I instantiate like this:

 DSingleton singleton = new DSingleton;

 Or like this:

 DSingleton singleton = singleton.get();

 And subsequent calls would be...? The same? Using get() only?
Sorry, I should read the post fully before replying, my bad. You access the singleton via the get() function whenever you need it. It is static so there's no need to create a copy of the instance in a "singleton" variable. DSingleton singleton = new DSingleton; is bad. It bypasses all the checks in the "get()" method to ensure it is a singleton and outside the module where you defined DSingleton it won't compile. bye, norm
Feb 02
prev sibling next sibling parent Tony <tonytdominguez aol.com> writes:
On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:
 So, my big question is, do I instantiate like this:

 DSingleton singleton = new DSingleton;

 Or like this:

 DSingleton singleton = singleton.get();

 And subsequent calls would be...? The same? Using get() only?
This seems to be a case where D's definition of "private" can cause confusion versus examples from other languages with private as "access only inside the class". In other languages DSingleton singleton = new DSingleton; would never compile (the desired behavior), since the constructor is private. But D allows it in some cases due to "private to class OR module". If DSingleton is in the same module, as for example in a small test program with DSingleton and main() in the same file, then it compiles. So I think your code is fine, it's just that it can be circumvented - the private constructor can be called - if DSingledton and the instantiating code are in the same module.
Feb 02
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2019-02-02 17:56, Ron Tarrant wrote:
 Hi guys,
 
 I ran into another snag this morning while trying to implement a 
 singleton. I found all kinds of examples of singleton definitions, but 
 nothing about how to put them into practice.
 
 Can someone show me a code example for how one would actually use a 
 singleton pattern in D? When I did the same thing in PHP, it took me 
 forever to wrap my brain around it, so I'm hoping to get there a little 
 faster this time.
You don't need to make it so complicated. Here's a simpler example: class DSingleton { private __gshared auto instance_ = new DSingleton; private this() // private to make sure no one else can create an instance { } static DSingleton instance() { return instance_; } } void main() { writeln(DSingleton.instance); } -- /Jacob Carlborg
Feb 03
parent reply Ron Tarrant <rontarrant gmail.com> writes:
On Sunday, 3 February 2019 at 18:53:10 UTC, Jacob Carlborg wrote:

 You don't need to make it so complicated. Here's a simpler 
 example:
Excellent. Thank you. Simple is best.
     private __gshared auto instance_ = new DSingleton;
My understanding is that in D, this line effectively says: the singleton is created at compile time and can't be changed, ever. Is that a fair assessment?
     private this() // private to make sure no one else can 
 create an instance
I've seen comments similar to this in several examples. When you say "no one else" you're personifying callers? And so this means: No caller outside the object? Which really amounts to: Since no one INside the object WILL call this() and no one OUTside CAN call this(), it won't ever get called.
     writeln(DSingleton.instance);
No '()' needed for the call to DSingleton.instance? If it's called again from somewhere else, say from within an object function several levels of scope away, it's called the same way?
Feb 04
next sibling parent Alex <sascha.orlov gmail.com> writes:
On Monday, 4 February 2019 at 10:36:49 UTC, Ron Tarrant wrote:
 On Sunday, 3 February 2019 at 18:53:10 UTC, Jacob Carlborg 
 wrote:

 You don't need to make it so complicated. Here's a simpler 
 example:
Excellent. Thank you. Simple is best.
     private __gshared auto instance_ = new DSingleton;
My understanding is that in D, this line effectively says: the singleton is created at compile time and can't be changed, ever. Is that a fair assessment?
No, it can. ´´´ class DSingleton { private __gshared auto instance_ = new DSingleton; size_t state; private this(){} // private to make sure no one else can create an instance static DSingleton instance() { return instance_; } } void main() { assert(DSingleton.instance.state == 0); DSingleton.instance.state = 5; assert(DSingleton.instance.state == 5); } ´´´
     private this() // private to make sure no one else can 
 create an instance
I've seen comments similar to this in several examples. When you say "no one else" you're personifying callers?
To some extent...
 And so this means: No caller outside the object? Which really 
 amounts to: Since no one INside the object WILL call this() and 
 no one OUTside CAN call this(), it won't ever get called.
I think, what is meant is: The class has to be placed alone in a module so that no one outside the class (and the module) has any direct access to object creation routine.
     writeln(DSingleton.instance);
No '()' needed for the call to DSingleton.instance? If it's called again from somewhere else, say from within an object function several levels of scope away, it's called the same way?
Yes. https://dlang.org/spec/function.html#optional-parenthesis
Feb 04
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2019-02-04 11:36, Ron Tarrant wrote:

 I've seen comments similar to this in several examples. When you say "no 
 one else" you're personifying callers? 
Yes.
 And so this means: No caller 
 outside the object? Which really amounts to: Since no one INside the 
 object WILL call this() and no one OUTside CAN call this(), it won't 
 ever get called.
Yes, well it's called once. Technically since the protections in D work on module level a caller inside the same file can access the constructor. But one is usually in control of the whole file.
     writeln(DSingleton.instance);
No '()' needed for the call to DSingleton.instance?
No need. It's the way D to implement properties.
 If it's called again from somewhere else, say from within an object 
 function several levels of scope away, it's called the same way?
You can call it with or without parentheses. It applies to all functions that don't take any arguments or functions taking a single argument and are called using UFCS [1]. [1] https://dlang.org/spec/function.html#pseudo-member -- /Jacob Carlborg
Feb 04
parent Ron Tarrant <rontarrant gmail.com> writes:
On Monday, 4 February 2019 at 19:23:26 UTC, Jacob Carlborg wrote:

 You can call it with or without parentheses. It applies to all 
 functions that don't take any arguments or functions taking a 
 single argument and are called using UFCS [1].

 [1] https://dlang.org/spec/function.html#pseudo-member
Okay, that explains a few things that I still hadn't caught onto. Thanks.
Feb 06
prev sibling next sibling parent reply Dejan Lekic <dejan.lekic gmail.com> writes:
On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:
 Hi guys,

 I ran into another snag this morning while trying to implement 
 a singleton. I found all kinds of examples of singleton 
 definitions, but nothing about how to put them into practice.

 Can someone show me a code example for how one would actually 
 use a singleton pattern in D? When I did the same thing in PHP, 
 it took me forever to wrap my brain around it, so I'm hoping to 
 get there a little faster this time.
I strongly suggest you find the thread started by Andrej Mitrovic many years ago. He compared several implementations of (thread-safe) singletons. I it an extremely helpful stuff, IMHO.
Feb 03
parent Ron Tarrant <rontarrant gmail.com> writes:
On Sunday, 3 February 2019 at 22:25:18 UTC, Dejan Lekic wrote:

 I strongly suggest you find the thread started by Andrej 
 Mitrovic many years ago. He compared several implementations of 
 (thread-safe) singletons. I it an extremely helpful stuff, IMHO.
Thanks. I'll see if I can find it.
Feb 04
prev sibling parent reply bauss <jj_1337 live.dk> writes:
On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:
 Hi guys,

 I ran into another snag this morning while trying to implement 
 a singleton. I found all kinds of examples of singleton 
 definitions, but nothing about how to put them into practice.

 Can someone show me a code example for how one would actually 
 use a singleton pattern in D? When I did the same thing in PHP, 
 it took me forever to wrap my brain around it, so I'm hoping to 
 get there a little faster this time.

 Here's the singleton code I've been playing with:

 class DSingleton
 {
 	private:
 	// Cache instantiation flag in thread-local bool
 	// Thread local
 	static bool instantiated_;

 	// Thread global
 	__gshared DSingleton instance_;

 	this()
 	{
 		
 	} // this()

 	public:
 	
 	static DSingleton get()
 	{
 		if(!instantiated_)
 		{
 			synchronized(DSingleton.classinfo)
 			{
 				if(!instance_)
 				{
 					instance_ = new DSingleton();
 					writeln("creating");
 				}

 				instantiated_ = true;
 			}
 		}
 		else
 		{
 			writeln("not created");
 		}

 		return(instance_);
 		
 	} // DSingleton()

 } // class DSingleton

 So, my big question is, do I instantiate like this:

 DSingleton singleton = new DSingleton;

 Or like this:

 DSingleton singleton = singleton.get();

 And subsequent calls would be...? The same? Using get() only?
Your code is not going to work in the way you think. Ex. you state "instantiated_" is thread-local but that's the flag you use to check whether it has been instantiated. That will not work. Instead it should actually be shared, especially because you use it in a synchronized statement. Also: DSingleton singleton = new DSingleton; Defeats the purpose of singleton.
Feb 04
parent kdevel <kdevel vogtner.de> writes:
On Monday, 4 February 2019 at 10:17:53 UTC, bauss wrote:
 On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:
[...]
 Here's the singleton code I've been playing with:
[...]
 	static bool instantiated_;

 	// Thread global
 	__gshared DSingleton instance_;
[...]
 		if(!instantiated_)
[...]
 				if(!instance_)
[...]
 Ex. you state "instantiated_" is thread-local but that's the 
 flag you use to check whether it has been instantiated.
User angel has pointed to https://davesdprogramming.wordpress.com/ and the talk https://www.youtube.com/watch?v=ZHmIAdlNtiM which entails the reasoning about that pattern.
 That will not work.
Will it not work in therory (in which?) or in practice (in which)?
 Instead it should actually be shared, especially because you 
 use it in a synchronized statement.
The purpose of the pattern is to avoid synchronisationin all but the first call to get().
 Also:

 DSingleton singleton = new DSingleton;

 Defeats the purpose of singleton.
Sure, but it is not lazy.
Feb 05