Design patterns are proven solutions to software design problems. They help improve code quality, promote reusability, and increase maintainability. We use them to save time and produce quality, extensible and flexible code. In this article, we are going to introduce the Singleton design pattern.
The Singleton pattern is a creational design pattern that ensures that a class has only one instance and provides only one access point to that instance.
The Singleton pattern includes the following elements:
The Singleton pattern ensures that only one instance of the Singleton class can be created and that there is only one way to access it. The benefit of this pattern is determined when we want to use the same pattern in different parts of the program.
The main difference between Singleton and Static classes is that a Singleton class allows you to create an instance of a class. Whereas a static class does not allow you to create any instance of the class. And all members are accessible using the class name.
In general, the Singleton pattern is useful when you need to manage a shared resource, such as a database connection, configuration data, or a hardware device, and you want to ensure that there is only one instance of the class that manages that resource. Static classes are useful when you have a set of utility methods that do not need to maintain state and can be called from any point in the code without having to create an instance of the class.
Below is an example of the Singleton pattern in C#:
public sealed class Singleton
{
private static Singleton instance = null;
private Singleton()
{
// Private constructor to prevent other instances from being created
}
public static Singleton GetInstance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
In this example, the Singleton class is defined as sealed to prevent inheritance and ensure that it can only have one instance. The instance variable is defined as private and static and is initialized with null value.
The GetInstance property/method is a public static property/method that provides access to the instance. Checks its value to make sure the instance has been created and returns it if an existing instance has already been created. To use the instance, you just need to call the Singleton.GetInstance property from anywhere in the application. Defining the Singleton class like previous example, is not safe in applications that use multiple threads.
In order to be able to use the Singleton pattern in multi-threaded programs, the code must be changed as follows:
public sealed class Singleton
{
private static Singleton instance = null;
private static readonly object padlock = new object();
private Singleton()
{
// Private constructor to prevent other instances from being created
}
public static Singleton GetInstance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
}
This change causes that if several threads want to access a common part of the memory at the same time, they cannot do it in parallel. Therefore, each thread must execute the code in the lock block in turn. As a result, the sample is set in the first thread and we will not enter the condition in the next threads.
To read about other design patterns, you can use the list below. There is also a code repository on GitHub that includes all the design patterns.
I am Reza Babakhani, a software developer. Here I write my experiences, opinions and suggestions about technology. I hope that what I write is useful for you.
leave a comment