www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Correct way to create singleton?

reply Konstantin Kutsevalov <adamasantares gmail.com> writes:
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
next sibling parent reply Anonymouse <asdf asdf.net> writes:
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
parent Konstantin Kutsevalov <adamasantares gmail.com> writes:
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!
Nov 10 2016
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
parent Jonathan M Davis via Digitalmars-d-learn writes:
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
prev sibling parent Royce <royceroyinfo gmail.com> writes:
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