Singleton Design Pattern
In some cases, it is important to have exactly one instance of a class. Although you have many electric appliance in your house but you have only one main switch box for incoming electricity in house. You have only one control panel in your windows Operating system. There is only one payroll software system to handle paychecks.
How do you ensure that you have only one instance of a class and that is also easily accessible? A global variable makes an object accessible but it does not prevent you from instantiating multiple objects.
A better solution is to make class itself responsible for keeping track of its single instance. The class can ensure that no other instance can be created and it can provide a way to access the instance. This is called Singleton Pattern.
Whenever you are looking for single point of connection to use at multiple places. Best example is logging. To access logging process in a program in various classes you don’t want to end up with creating objects of logger class and access methods. Rather you would prefer to have one instance of class and use everywhere in solution or project. Thus in Singleton pattern, Information flows in one way.
Client can access a Singleton instance solely through Singleton’s Instance operation. Instance is a class operation similar to static member function in C++.
The Singleton pattern has several benefits.
1. Controlled access to its solo instance: Singleton class encapsulates its solo instance, which has strict control over how and when client can access it.
2. Flexibility over instances: Because the class controls the instantiation process, the class has the flexibility to change the instantiation process too.
1. Overhead: Though the is minimal time but still there is some overhead involving to check whether an instance class already exists every time an object request a reference.
2. Object lifetime:
HOW TO DESIGN?
Following implementation is singleton Pattern given in MSDN
Look at the code, you will notice at line 5; class constructor is defined as private which ensures that no other object can create a new instance of this Singleton class.
This implementation has two advantages:
1. Because instance has created inside the instance property method, the class can perform another actions as well like instantiating subclasses, though it may introduce unwanted dependencies.
2. Instantiation is not performed until an object asks for the instance.
The disadvantage of this pattern is that it is not safe in multi threaded environment. If at the same time, two separate thread ask for instance, it will check at line 10 before creating a instance. At this point, there is a possibility of race condition. To avoid that, Static Initialization is the solution.
In this strategy, member of the class is creating the instance first time inside class itself. The Common Language Runtime (CLR) will take care of variable initialization. The class is marked sealed to prevent any derivation. The variable marked as read-only, which means that it can be assigned only during static initialization or in the class constructor.
For detailed implementation of Singleton Design Pattern refer the section 4.
1. Design Pattern: Element of Reusable Object-Oriented Software By: Erich Richard Ralph and John.
2. MSDN Library.