Design Pattern is nothing but a reusable solution & interactions of objects to the complex software design problem that we can face every day in programming.
Singleton Design Pattern
This means only one instance of the particular class provides a global point of access to that instance and used for all of the entire applications.
In this article, we are going to explore only the Singleton design pattern. this belongs to the creational type design pattern.
simply we can say like, we are going to create one object and used it at the global level. simply it comes under creational design pattern type which means it deals with object creation mechanism.
Point To Remember:
- Only one object of a particular class is instantiated.
- that single instance created is responsible to coordinate across the app
Advantages of Singleton Design Pattern
- It can be Lazy loading
- It has static initialization
- Singleton design patterns can be extended into the factory design pattern.
- The main advantage is saving the memory which means not created the object each and every time.
- only one instance is used again and again at the entire application.
- Used in Logging & Multithreaded
- used in Configuration setting like Database connection etc.
- Caching and thread pools.
Example Program With Explanation
Below is the code without a singleton design pattern. here we are going to create one main class for creating objects and a second class for a singleton class.
Main Class
This class calls the singleton class and initialization.
using System; namespace ConsolePractice {
class Program
{
static void Main(string[] args)
{ Singleton_Design_Pattern obj = new Singleton_Design_Pattern(); obj.getEmployee("Murali
Krishna");
Singleton_Design_Pattern obj1 = new Singleton_Design_Pattern(); obj.getEmployee("Siva"); Console.ReadLine();
}
} } |
---|
Singleton Class
Class with one default constructor and one method for testing.
|
---|
The above class tells the object creation. when we call the singleton class from the main class for each and every time an object is creating. so we are allocating the memory 2 times in heap memory if the memory allocation is more in heap memory then the application performance reduces gradually. when comes to production this scenario makes huge losses for the client.
How to Create Singleton Class in C#
If we want to achieve this then we need to follow only one instance. how we can achieve one instance by using the static keyword. why static keyword. it creates the memory in the Stack.
Why static?
- The keyword static indicates that only one instance of the member exists for a class.
- Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it.
Steps to implement the Class.
Step 1:
- Restrict the multiple instance creation by changing the public to the private default constructor and class as a Sealed class to avoid the inheritance.
Step 2:
- Now go to the singleton class and create static members. in that class and initialize the singleton class instance as null and return it like the below snippet.
private static Singleton_Design_Pattern
singleton = null;
Step 3:
- Create static constructor get property
Singleton Class
the below class modified as per single instance creation. the class only one instance created and reused whenever we want to use that class. and the class is Sealed and class members are protected by declaring the default constructor as Private. and we are returning the instance property.
|
---|
Main Class
After modifying the that class then we need to call by the main class. how to class from the main class. simple by using "." because we declared it as static and constructor as privateusing System; namespace ConsolePractice {
class Program
{
static void Main(string[] args)
{ Singleton_Design_Pattern obj = Singleton_Design_Pattern.GetNewInstance; obj.getEmployee("Murali
Krishna"); Singleton_Design_Pattern obj1 = Singleton_Design_Pattern.GetNewInstance; obj.getEmployee("Siva"); Console.ReadLine();
}
} } |
---|
the output looks like below image
Conclusion
So finally we achieved the singleton design pattern but it can't handle the multithreaded situation and lazy loading because it is a single thread trying to create an instance of that class.
Post a Comment (0)