digitalmars.D.learn - Correct way to create singleton?
- Konstantin Kutsevalov (24/24) Nov 10 2016 Hi, what is a correct (and simple) way to create an singleton?
- Anonymouse (3/4) Nov 10 2016 https://wiki.dlang.org/Low-Lock_Singleton_Pattern
- Konstantin Kutsevalov (2/6) Nov 10 2016 great!
- Steven Schveighoffer (6/23) Nov 10 2016 There's a specific way to create a singleton that David Simcha outlined
- Jonathan M Davis via Digitalmars-d-learn (7/11) Nov 10 2016 And what's particular cool about it from the perspective of promoting D ...
- Royce (12/36) Nov 16 2016 Hi Guys
Hi, what is a correct (and simple) way to create an singleton? This is how I see that now: ``` class ApMessageRouter { static ApMessageRouter instance = null; private this() { } // for disable constructor to use from outside public ApMessageRouter getInstance() { if (this.instance is null) { this.instance = new ApMessageRouter(); } return this.instance; } } ``` Thank you.
Nov 10 2016
On Thursday, 10 November 2016 at 17:17:51 UTC, Konstantin Kutsevalov wrote:Hi, what is a correct (and simple) way to create an singleton?https://wiki.dlang.org/Low-Lock_Singleton_Pattern
Nov 10 2016
On Thursday, 10 November 2016 at 17:22:48 UTC, Anonymouse wrote:On Thursday, 10 November 2016 at 17:17:51 UTC, Konstantin Kutsevalov wrote:great!Hi, what is a correct (and simple) way to create an singleton?https://wiki.dlang.org/Low-Lock_Singleton_Pattern
Nov 10 2016
On 11/10/16 12:17 PM, Konstantin Kutsevalov wrote:Hi, what is a correct (and simple) way to create an singleton? This is how I see that now: ``` class ApMessageRouter { static ApMessageRouter instance = null; private this() { } // for disable constructor to use from outside public ApMessageRouter getInstance() { if (this.instance is null) { this.instance = new ApMessageRouter(); } return this.instance; } } ``` Thank you.There's a specific way to create a singleton that David Simcha outlined in a talk at dconf 2013, that avoids all the race condition issues that singletons traditionally have: https://davesdprogramming.wordpress.com/2013/05/06/low-lock-singletons/ -Steve
Nov 10 2016
On Thursday, November 10, 2016 14:25:49 Steven Schveighoffer via Digitalmars-d-learn wrote:There's a specific way to create a singleton that David Simcha outlined in a talk at dconf 2013, that avoids all the race condition issues that singletons traditionally have: https://davesdprogramming.wordpress.com/2013/05/06/low-lock-singletons/And what's particular cool about it from the perspective of promoting D is that it's _very_ easy to implement in D, whereas in most other languages, it involves using constructs that many folks don't even know exist, let alone know how to use (in particular, you need to use TLS). - Jonathan M Davis
Nov 10 2016
On Thursday, 10 November 2016 at 17:17:51 UTC, Konstantin Kutsevalov wrote:Hi, what is a correct (and simple) way to create an singleton? This is how I see that now: ``` class ApMessageRouter { static ApMessageRouter instance = null; private this() { } // for disable constructor to use from outside public ApMessageRouter getInstance() { if (this.instance is null) { this.instance = new ApMessageRouter(); } return this.instance; } } ``` Thank you.Hi Guys Singleton pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is pattern is one of the simplest design patterns. This pattern ensures that a class has only one instance. In this article, I would like share some useful link and helpful link. I hope it will help you to implementing singleton in correct way. https://www.mindstick.com/forum/33927/how-to-implement-singleton-design-pattern-in-c-sharp http://blogs.tedneward.com/patterns/Singleton-CSharp/ Thanks
Nov 16 2016