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 reply =?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
parent reply Suliman <evermind live.ru> writes:
On Thursday, 5 January 2012 at 22:53:12 UTC, Ali Çehreli wrote:
 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
Yeah, same question, what difference between singleton and `static this()`?
Jul 09 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 07/09/2016 01:35 PM, Suliman wrote:
 On Thursday, 5 January 2012 at 22:53:12 UTC, Ali Çehreli wrote:
 On 01/05/2012 02:15 PM, asm wrote:
 how can i implementing the singleton pattern in D?
Is singleton still alive? ;)
I'm glad I was alive in 2012. :o)
 An idea is to instantiate the object in the module's static this().

 Ali
Yeah, same question, what difference between singleton and `static
this()`? I'm now aware of the difference between 'static this()' and 'shared static this()'.: shared static this(): Executed once for the whole program static this(): Executed per thread Ali
Jul 09 2016
parent Suliman <evermind live.ru> writes:
On Saturday, 9 July 2016 at 20:47:32 UTC, Ali Çehreli wrote:
 On 07/09/2016 01:35 PM, Suliman wrote:
 On Thursday, 5 January 2012 at 22:53:12 UTC, Ali Çehreli
wrote:
 On 01/05/2012 02:15 PM, asm wrote:
 how can i implementing the singleton pattern in D?
Is singleton still alive? ;)
I'm glad I was alive in 2012. :o)
 An idea is to instantiate the object in the module's static
this().
 Ali
Yeah, same question, what difference between singleton and
`static this()`? I'm now aware of the difference between 'static this()' and 'shared static this()'.: shared static this(): Executed once for the whole program static this(): Executed per thread Ali
Yeah, I understand, I mean that I need way to create instance on Config class, that should not be passed in every instance of class explicitly, because it would need in every class. I know that I can make it's static, but I do not think, that it's good. Same with singleton, I read that a lot of people blame it.
Jul 09 2016
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:
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?
Jan 11 2012
prev sibling next 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
prev sibling parent yawniek <dlang srtnwz.com> writes:
On Thursday, 5 January 2012 at 22:15:32 UTC, asm wrote:
 how can i implementing the singleton pattern in D?
https://p0nce.github.io/d-idioms/#Leveraging-TLS-for-a-fast-thread-safe-singleton
Jul 10 2016