The Singleton Design Pattern in C# falls under the creational pattern category that deals with object creation and manipulation. Singleton design patterns ensure that only one instance of a class is created. The singleton class returns the same instance each time whenever requested from outside the class.
What are the advantages of using Singleton Design Pattern in C#:
- Singleton class provides the concurrent access to shared resources.
- Singleton class provides a 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 well.
What are the Singleton pattern design principles?
- The Singleton class should have one private static class object of the same class. The object holds the reference to the same class.
- The Singleton class should have a public static method/property that creates the singleton instance of the same class. The instance will be created only once and return the reference on demand. Further, the method/property checks whether the instance is created and returns the same instance if it is already created. Otherwise, the method/property will create and return the singleton instance.
This is called lazy initialization.
- the constructor of the Singleton class should be declared as private without any parameters. So, this prevents creating the instance from outside the class.
- Declare singleton class as sealed to prevent inheritance from any derived classes.
There are two ways you can create a singleton instance of the same class. The first way is by using static property and secondly by using a static method whose job is to create the singleton instance only once and then return that singleton instance each and every time when we call that public static property/method from outside the singleton class. The below example shows the Singleton object created via lazy initialization where the GetInstance property/method returns 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.
What is Sealed Singleton Class?
The best practice to use the Singleton class is that it should not be derived from any other class and not instantiated from outside. This means that you should declare the Singleton class constructor as private to prevent creating the instance of the same class from outside. Further, if you try to instantiate the Singleton class from outside or derive the Singleton class, then you will receive a 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 a nested class inside Student Class. Hence it is possible DerivedStudent class to inherit Student class.
Now, declare Singleton class as sealed that prevents 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 throws a compilation error saying that sealed class can not be inherited. So, the best practice is to mark Singleton’s class as sealed.
What is Thread Safe Singleton Class?
In a multithreading environment, there may be chances where two or more threads may access the singleton class that creates instances of the same class parallel. To avoid this duplication, the Singleton pattern should be implemented by using Locks. A 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.
How to create Singleton Class in optimized way?
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 type:
The .Net 4 or higher introduced a better way of creating singleton instances via System.Lazy type to make laziness very simple. Here is an example.
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.
– Article ends here –