Hello Mark,
Thanks for posting in the group.
Based on my understanding, now the question is: How to declare and use a
singleton object in C#? Please correct me if I have misunderstood it.
Generally speaking, the intent of the Singleton pattern as defined in
Design Patterns is to "ensure a class has only one instance, and provide a
global point of access to it". The model for a singleton is very
straightforward. There is (usually) only one singleton instance. Clients
access the singleton instance through one well-known access point. The
client in this case is an object that needs access to a sole instance of a
singleton.
We can refer to a simple C++ singleton sample:
// Declaration
class Singleton {
public:
static Singleton* Instance();
protected:
Singleton();
private:
static Singleton* _instance;
}
// Implementation
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance() {
if (_instance == 0) {
_instance = new Singleton;
}
return _instance;
}
Let's examine this code for a moment. This simple class has one member
variable and that is a pointer to itself. Notice that the constructor is
protected and that the only public method is the Instance method. In the
implementation of the Instance method, there is a control block (if) that
checks to see if the member variable has been initialized, and if not
creates a new instance. This lazy initialization in the control block means
that the Singleton instance is initialized, or created, only on the first
call to the Instance() method. For many applications, this approach works
just fine. But, for multithreaded applications, this approach proves to
have a potentially hazardous side effect. If two threads manage to enter
the control block at the same time, two instances of the member variable
could be created. To solve this, you might be tempted to merely place a
critical section around the control block in order to guarantee thread
safety. If you do this, then all calls to the Instance method would be
serialized and could have a very negative impact on performance, depending
on the application.
Microsoft .NET Framework has addressed all of these issues, thus making it
easier to implement a singleton without the adverse side effects we
discussed thus far. The following sample uses .NET, is a minimal Singleton
class based loosely on the original GoF pattern, and still gets similar
behavior.
// .NET Singleton
sealed class Singleton
{
private Singleton() {}
public static readonly Singleton Instance = new Singleton();
}
The Framework internally guarantees thread safety on static type
initialization. In other words, in the example above, there is only one
instance that would ever be created of the Singleton class.
For the usage of this class, please refer to the following:
Sample Singleton Usage
sealed class SingletonCounter {
public static readonly SingletonCounter Instance =
new SingletonCounter();
private long Count = 0;
private SingletonCounter() {}
public long NextValue() {
return ++Count;
}
}
class SingletonClient {
[STAThread]
static void Main() {
for (int i=0; i<20; i++) {
Console.WriteLine("Next singleton value: {0}",
SingletonCounter.Instance.NextValue());
}
}
}
For details on this topic, please refer to MSDN article "Exploring the
Singleton Design Pattern" at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/
singletondespatt.asp.
Does that answer your question?
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ¨C
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.