Correct way to create singleton?

Royce via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 16 00:30:39 PST 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


More information about the Digitalmars-d-learn mailing list