Singleton Design Pattern
The Singleton Design Pattern is a creational design pattern that ensures that only one instance of the class can be created and provides a global point of access to it.
UML Diagram for Singleton
Based on the preceding UML diagram, I can convert it to a C# class as in the following.
Singleton.cs
- namespace PrashantBlogs.SingletonPattern {
-
-
-
-
- sealed class Singleton {
- private static Singleton _instance = null;
-
-
-
- private Singleton() {}
-
- static internal Singleton Instance() {
-
-
- if (_instance == null) {
- _instance = new Singleton();
- }
- return _instance;
- }
- }
The preceding class is a sealed class having a private constructor that strictly prohibits the object creation from outside of this class.It has a static method marked with the internal keyword and this method allows the creation of the instance of this class if the instance is not already created.For the testing of the preceding class, you can use the following code snippet that is a main program written for creating a single instance using the Singleton Pattern.Program.cs
- using System;
-
- namespace PrashantBlogs.SingletonPattern {
- class Program {
- static void Main(string[] args) {
- Singleton obj1 = Singleton.Instance();
- Singleton obj2 = Singleton.Instance();
- if (obj1 == obj2) {
- Console.WriteLine("We both are once instance having two names");
- }
- Console.ReadKey();
- }
- }
- }
Output
Now, if I were a beginner-level programmer, I would have raised the following questions on the preceding example.
- What is a Design Pattern?
- What is a Creational Pattern?
- Why is a Singleton class be marked as sealed?
- Why is the default constructor of a Singleton class marked private?
- In a Singleton class, why is the Instance method marked static internal?
Of course, the questions could have been many, but I felt the preceding ones are the most obvious. The answers to these questions are as in the following.
- It's a general reusable solution to a commonly occurring problem within a given context in software design. It's not a finished design that you can incorporate your source code directly, but it's a template for how to solve a problem that can be used in many situations. It's a documented solution and formalized as a best practice.
- The creational pattern deals with object creation mechanisms. There are other design patterns following under this category such as Factory Method, Abstract Factory, Prototype and Builder and are used in situations when the basic form of object creation could result in design problems or increased complexity of a code base.
- The sealed keyword is used to restrict our Singleton class from being inherited.
- The default constructor of the Singleton class has been marked as private in order to restrict the object creation from outside of this class. If it is marked as public then multiple instances of this class could be created and it would not be a singleton since it is necessary to ensure that only one object can be created that should cater for client calls.
- The Instance method has been marked as static internal to make it available to the classes that is a part of the same assembly where the Singleton class resides.
If you want our Singleton class and the Instance method to be accessed from other assemblies or projects that are referencing the assembly containing our Singleton class, then of course you can mark our Singleton class public. Just add the keyword public before the sealed keyword of our Singleton class and also replace the static internal keywords from the Instance method with public static and the class is ready to be used in other assemblies or projects also.
This example of the Singleton pattern is not thread safe. In the next parts of my article, I will cover more examples of the Singleton pattern along with the real world and thread safe examples also. Your feedback will help me in further improvements context-wise so please feel free to post it.