In C#, the Singleton Design Pattern falls under the creational pattern category that deals with object creation and manipulation. Singleton design pattern ensure that only one instance of a class is created. The singleton class return the same instance each time when ever request from outside the class.
Advantages of using Singleton Pattern in C#:
- Singleton class provides the concurrent access to shared resources.
- Singleton class providing single point of access to the instance globally. Hence it is easy to maintain.
- It reduces the repeated object creation. So, application performance will be maintained good.
Singleton pattern design principles:
- The Singleton class should have one private static class object of the same class. The object hold the reference to the same class.
- The Singleton class should have public static method/property that create the singleton instance of the same class. The instance will be created only once and return the reference on demand. Further, the method/property check whether the instance is created and return the same instance if it is already created. Otherwise, the method/property will create and return the singleton instance.
- The constructor of the Singleton class should be declared as private with out any parameters. So, this prevents creating the instance from outside the class.
- Declare singleton class as sealed to prevent inherit from any derived classes.
This is called lazy initialization.
There are two ways you can create singleton instance of the same class. The first way by using static property and secondly by using static method whose job is to create the singleton instance only once and then return that singleton instance each and every time when we called that public static property/method from outside the singleton class. Below example shows Singleton object created via lazy initialization where the GetInstance property/method return the object only after the instance is created.
In this example, see how you can create singleton instance of a class by using static property. The property GetInstance return the single instance of Student class. The instance of Student class is stored in static object oStudent.
In this example, see how you can create singleton instance of a class by using static method. The method GetInstance() return the single instance of Student class. The instance of Student class is stored in static object oStudent.
Sealed Singleton Class
The best practice to use Singleton class is that should not be derived from any other class and not instantiated from outside. This means that, Singleton class constructor should be declared as private to prevent creating the instance of the same class from out side. Further, if you try to instantiate the Singleton class from outside or derive the Singleton class then you will receive compilation error. In the below example the DerivedStudent class is trying to inherit Student class and compilation error occurred.
But still the nested class can inherit Singleton class even though the constructor is marked as private. In the below example, the DerivedStudent class is created as nested class inside Student Class. Hence it possible DerivedStudent class to inherit Student class.
Now, declare Singleton class as sealed that prevent singleton class inherited by any nested class. In the below example the class Student declared as sealed class and DerivedStudent class is nested inside Student class. This throw compilation error saying that sealed class can not be inherited. So, the best practice is to mark Singleton class as sealed.
Thread Safe Singleton Class
In multithreading environment there may be chances where two or more threads may access the singleton class that create instance of the same class parallel. To avoid this duplication, Singleton pattern should be implemented by using Locks. Lock object is required to control and prevent the multiple threads race condition. In the below example the GetInstance() method introduced Lock before creating and returning the singleton instance of Student class.
Optimized way of creating Singleton Class:
In the below example, The singleton class Student has readonly static class object that create the single instance immediately when class is loaded. .Net guarantees the thread safety for static initialization. In the next section you can see one more method to create singleton class that is again a thread safe.
Creating Singleton instance using System.Lazy
The .Net 4 or higher introduced a better way of creating singleton instance via System.Lazy
Singleton realtime scenarios
- Entity Classes that hold the application level data globally.
- Facades Classes that implement database operation that interact with database, application specific facades.
- Service Classes that implement services such as FTP, Proxy classes, NTP and more.
- Log Classes that create the diagnostic logs for your applications.
- Data Sharing Classes that hold configuration information either from database of from configuration file.
Software engineer by profession, founder of troubleshotyourself channel. Enthusiastic blogger and love to write articles on new technology, programming, networking and more. Reach me at firstname.lastname@example.org.