In this series, we try to explore software design patterns and principles. We will try to learn the well-known OOP design patterns one by one. In this part, we try to explore the singleton design pattern.
Ismael is a very good boy and over 21 years old. As he is an adult now, he decided to marry someone and addressed him as my wife. As I previously stated, he is a good boy (though, like other men, he also has petty thinking), and he desires only one wife in his entire life. So he is looking for a wife until he finds someone to be his wife. When he found someone, he left his entire life with her and refused to accept any other girls as his wife.
The singleton design pattern is a creational design pattern that helps us to create and maintain only one instance of a class. That is, as its name refers, the singleton design pattern gives us a way to create only one instance object of a class and ensure the use of this object everywhere in the system.
Like the Ismael in the above story, he allows the first woman as his wife; after that, he refuses the other instances of women as his wife.
Use the Singleton pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program or a logger for log systems’ different messages.
Use the Singleton pattern when you need stricter control over global variables.
using System;
namespace SingletonDesignPattern
{
sealedclassWife {
// Step 1: Create a single, readonly instance at class load timeprivatestaticreadonly Wife instance = new Wife();
// Step 2: Track number of instances (should always be 1)publicstaticint NumberOfInstance { get; privateset; }
// Step 3: Private constructor prevents external instantiationprivate Wife()
{
NumberOfInstance++;
Console.WriteLine("Wife instance created.");
}
// Step 4: Provide a global access pointpublicstatic Wife Instance
{
get { return instance; }
}
}
classProgram {
staticvoid Main(string[] args)
{
// Access singleton instance Wife wife1 = Wife.Instance;
Console.WriteLine($"Number of instances: {Wife.NumberOfInstance}");
// This line would cause a compile-time error because the constructor is private// Wife obj = new Wife(); Wife wife2 = Wife.Instance;
Console.WriteLine($"Number of instances: {Wife.NumberOfInstance}");
// Confirm both references point to the same instance Console.WriteLine($"Are wife1 and wife2 same instance? {ReferenceEquals(wife1, wife2)}");
}
}
}
The above implementation is fine and simple enough, but here is a problem when we are working on a multithread. Because in the multithread system we can’t guarantee 100% that only one instance will be created because more than 1 thread can access the class simultaneously and create multiple instances of the class and break the singleton rule. So we have a thread-safe implementation of the singleton design pattern as below.
We use cookies and technologies like Google Analytics and
Microsoft Clarity to understand how users interact with our site
and improve your experience.