www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Singleton Pattern

reply asm <asm hotmail.com> writes:
how can i implementing the singleton pattern in D?
Jan 05 2012
next sibling parent reply Andrew Wiley <wiley.andrew.j gmail.com> writes:
On Thu, Jan 5, 2012 at 4:15 PM, asm <asm hotmail.com> wrote:
 how can i implementing the singleton pattern in D?

Multithreaded or not?
Jan 05 2012
parent asm <asm hotmail.com> writes:
both if you don't mind
Jan 05 2012
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/05/2012 02:15 PM, asm wrote:
 how can i implementing the singleton pattern in D?

Is singleton still alive? ;) An idea is to instantiate the object in the module's static this(). Ali
Jan 05 2012
prev sibling next sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, January 05, 2012 22:15:32 asm wrote:
 how can i implementing the singleton pattern in D?

The same way that you would in C++ or Java, except that unlike you have static constructors that you can use to initialize them, and unlike both, if it's not shared, then it's a singleton per thread and no locking is required. If you're dealing with a multi-threaded singleton, technically, it should be possible to use double-checked locking with shared, but apparently bugs with shared make that unsafe at present. So, if you didn't initialize it in a shared static constructor, either you lock every time that you fetch the singleton or you use an extra bool for indicating that it's been initialized. Single threaded: T singleton() { static T instance; if(!instance) instance = new T(); return instance; } Multi-threaded: shared(T) singleton() { static shared T instance; static bool initialized = false; if(!initialized) { synchronized { if(!instance) instance = new shared(T)(); } initialized = true; } return instance; } Once the bugs with shared with regards to fences have been fixed though, the shared version can look more like shared(T) singleton() { static shared T instance; if(!instance) { synchronized { if(!instance) instance = new shared(T)(); } } return instance; } Regardless, if you use a shared static constructor, you can avoid the locking (it's just not lazily initialized then), and of course, the singly threaded one can use a static constructor if you don't care about lazy initialization. If you use a static constructor though, the instance would not be declared inside the singleton function (and it doesn't have to be here either - it's just shorter to do it this way in an example). - Jonathan M Davis
Jan 05 2012
parent asm <asm hotmail.com> writes:
thank you, you answer is very helpful.
Jan 06 2012
prev sibling next sibling parent Thomas Mader <thomas.mader gmail.com> writes:
--14dae9340a67ebc11904b6495c6c
Content-Type: text/plain; charset=ISO-8859-1

On Friday, January 6, 2012, Jonathan M Davis <jmdavisProg gmx.com> wrote:
 The same way that you would in C++ or Java, except that unlike you have

The most simple and elegant way in java is to use an enum. Is there nothing comparable in D? --14dae9340a67ebc11904b6495c6c Content-Type: text/html; charset=ISO-8859-1 <br><br>On Friday, January 6, 2012, Jonathan M Davis &lt;<a href="mailto:jmdavisProg gmx.com">jmdavisProg gmx.com</a>&gt; wrote:<br>&gt; The same way that you would in C++ or Java, except that unlike you have static<br><br> The most simple and elegant way in java is to use an enum. Is there nothing comparable in D? --14dae9340a67ebc11904b6495c6c--
Jan 11 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, January 12, 2012 00:55:14 Thomas Mader wrote:
 On Friday, January 6, 2012, Jonathan M Davis <jmdavisProg gmx.com> wrote:
 The same way that you would in C++ or Java, except that unlike you have

static The most simple and elegant way in java is to use an enum. Is there nothing comparable in D?

No, because enums must be known at compile time. This avoids the ordering issues that you get in languages like C++ or Java when you directly initialize static variables like that. So, to have an enum which is a class, you need to be able to construct a class at compile time and have it persist into the runtime of the program. And while you _can_ now use classes with CTFE, they cannot persist beyond CTFE (though that may be possible eventually). Any class which is used at runtime must be constructed at runtime. So, either you need to use a static constructor, or you need to initialize the singleton lazily. - Jonathan M Davis
Jan 11 2012