Singleton Design Pattern 
Author Message
 Singleton Design Pattern

I just finished reading the article on Exploring The
Singleton Design Pattern on MSDN:

http://www.*-*-*.com/
us/dnbda/html/singletondespatt.asp?frame=true

My question is in regards to the final sample code they
use for a simplified Singleton class.

sealed class SingletonCount
{
  public static readonly SingletonCounter Instance =
    new SingletonCounter();
  private long Count = 0;
  private SingletonCount() {}
  public long NextValue() { return ++Count; }

Quote:
}

What is the point of creating an internal instance of
itself as opposed to declaring Count and NextValue as
static?  As in the following code:

sealed class SingletonCount
{
  private static long Count = 0;
  private SingletonCount() {}
  public static NextValue() { return ++Count; }

Quote:
}

I tested this with a client app and the results appear to
be the same.  Also, I launched several client apps and
neither provided the expected behaviour of using the same
instance and keeping the same count between each client
app.  Each client app started it's own count and 0 and
incremented from one there on, as opposed to picking up
the count where the last one left off.  What gives?

TIA~ PJ



Mon, 06 Dec 2004 05:57:41 GMT  
 Singleton Design Pattern
I didn't look at the sample. But the point of having a public static
instance of itself is that it is guaranteed to be unique in the process.
Basically, nobody can create an instance of the class, the only instance is
SingletonCount.Instance. The magic here is the "private" keyword in front of
the class's constructor.

From anywhere in your code, you can only call
SingletonCount.Instance.NextValue().

In the second case, anywhere in your code you can do:
SingletonCount c = new SingletonCount();
c.NextValue();

this doesn't guarantee the sequence of the count because the SingletonCount
instance is not unique.

This is "singleness" at the process level. Starting multiple clients starts
multiple instance of the singleton class. To have a computer-wide singleton
you can use a singleton COM object.


Quote:
> I just finished reading the article on Exploring The
> Singleton Design Pattern on MSDN:

> http://msdn.microsoft.com/library/?url=/library/en-
> us/dnbda/html/singletondespatt.asp?frame=true

> My question is in regards to the final sample code they
> use for a simplified Singleton class.

> sealed class SingletonCount
> {
>   public static readonly SingletonCounter Instance =
>     new SingletonCounter();
>   private long Count = 0;
>   private SingletonCount() {}
>   public long NextValue() { return ++Count; }
> }

> What is the point of creating an internal instance of
> itself as opposed to declaring Count and NextValue as
> static?  As in the following code:

> sealed class SingletonCount
> {
>   private static long Count = 0;
>   private SingletonCount() {}
>   public static NextValue() { return ++Count; }
> }

> I tested this with a client app and the results appear to
> be the same.  Also, I launched several client apps and
> neither provided the expected behaviour of using the same
> instance and keeping the same count between each client
> app.  Each client app started it's own count and 0 and
> incremented from one there on, as opposed to picking up
> the count where the last one left off.  What gives?

> TIA~ PJ



Mon, 06 Dec 2004 09:27:09 GMT  
 Singleton Design Pattern

Quote:

> I didn't look at the sample. But the point of having a public static
> instance of itself is that it is guaranteed to be unique in the process.
> Basically, nobody can create an instance of the class, the only instance is
> SingletonCount.Instance. The magic here is the "private" keyword in front of
> the class's constructor.

> From anywhere in your code, you can only call
> SingletonCount.Instance.NextValue().

> In the second case, anywhere in your code you can do:
> SingletonCount c = new SingletonCount();
> c.NextValue();

No you can't.  The constructor is private.

Jim S.



Tue, 07 Dec 2004 03:54:12 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Singleton Design Pattern Question

2. How to implement Singleton pattern

3. Destructors/Singleton Pattern

4. Globalness of Singleton pattern

5. Singleton design approach

6. Proper Singleton Design in C#

7. Design patterns

8. FS: Design Patterns (GoF) book

9. Looking for a design pattern...

10. C and design patterns

11. Design Patterns in C#

12. Seeking Book Review - C# Design Patterns

 

 
Powered by phpBB® Forum Software