Definition
Ensure a class has only one instance and provide a global point of access to it.
With the Singleton Design Pattern in place, the developer can easily access that single object instance without needing to worry about inadvertently creating multiple instances, and provides a global point of access to it.
UML class diagram
Participants
The classes and/or objects participating in this pattern are:
Singleton
- defines an Instance operation that lets clients access its unique instance. Instance is a class operation.
- responsible for creating and maintaining its own unique instance.
Sample code in C#
public class SingletonSample
{
//shared members
private static SingletonSample _instance = new SingletonSample();
public static SingletonSample Instance()
{
return _instance;
}
//instance members
private SingletonSample()
{
//public instantiation disallowed
}
//other instance members
//...
}
-
-
Lectures(732)
-
Permalien