Thread overview
Correct way to create singleton?
Nov 10, 2016
Anonymouse
Nov 10, 2016
Jonathan M Davis
Nov 16, 2016
Royce
November 10, 2016
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.
November 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

November 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:
>> Hi, what is a correct (and simple) way to create an singleton?
>
> https://wiki.dlang.org/Low-Lock_Singleton_Pattern

great!
November 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
November 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

November 16, 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