Singleton Pattern
This pattern comes under the creational patterns which are widely used in programming constructs. This pattern provides the best way to create objects.
In simple words, the Singleton pattern provides the single instance of the class throughout the lifecycle of the execution. Singleton patterns consist of the single class which create the Instance of that class, a single instance used by the application throughout the lifecycle.
Explanation
- In many programming constructs sometimes we need only one object of the class to be used by the Application throughout the lifecycle.
- In Singleton pattern there is only one class which is responsible for the creation of the single instance
- Only one private and parameter-less constructor, the private constructor, ensures that no other classes call the constructor of that class to create the object. Another important thing about parameter-less constructor is that no parameter is passing to that constructor because if we make it private there is no way to pass the parameter.
- The Singleton class must be sealed/final so that class is not inherited by another class
- A static and read-only variable which is able to hold the reference of the instance
- Getter method in case of Java and Property in case of C# is a public static method or public property which is responsible for getting the instances.
Example and Implementation
- As I mentioned earlier, a good and practical example for Singleton is logging, configuration settings, thread pool, and device driver objects.
- There are many implementations of the Singleton design pattern.
UML Diagram
Code
- First of all, we create the C# console project and then add the class named as “Singleton.”
- The Singleton class is the class which uses the Singleton design pattern by creating the private static field with return type of that class (Singleton in my case) and then a private constructor and then a static public property which is responsible to get the instance of that class -- a single object.

- This was the main program.cs class.






Asad AliPosted Jun 15, 2016, 9:57 AM
hello tufail .. should we also use the sealed keywork?