Correct way to create singleton?

Konstantin Kutsevalov via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 10 09:17:51 PST 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.


More information about the Digitalmars-d-learn mailing list