Singleton Design Pattern

Singleton Design Pattern is one of the simplest and commonly used design patterns in object oriented design. Implementation of singleton classes ensures that only one instance of a class can be created. Please check out my latest video on this design pattern.

All the details are in the video. This blog post reiterates a few concepts that are discussed in this video and also includes source code that is used in the video.

Firstly, why does one need a singleton class? If an instance of a class does not hold any state that makes it distinguishable from other instances, that class becomes a candidate for Singleton. This becomes compelling in situations where class instantiation is a costly process. In general, I prefer to keep my controller classes stateless and those are generally declared singleton. There are posts on the internet that advocate static classes in place of singleton. My personal opinion is that static classes are easier to implement but static classes and methods have other shortcomings and that is why my preference is still singleton over static. This discussion is out of scope of this post and so I will cover my opinion in another post.

How do you make a class singleton?

a. Make it’s constructor private

b. Expose it’s only instance via a static property

c. Implement the ICloneable interface and throw an exception in the Clone method. This ensures that another program cannot create multiple instances of this program by calling ClassName.SingletonInstance.Clone().

Even though steps a – c appear fairly standard and easy to implement, there is another concern here which causes Singleton implementation to be problematic – thread safety.

In the YouTube video, I have taken the example of a “global setting” class that stores global settings for an application that are initialized once and never updated as long as the application runs. This is the implementation of a singleton class.

public class GlobalSettings: ICloneable
{
//the only instance of this class is created just once
private static readonly GlobalSettings SingletonInstance
= new GlobalSettings();
 
// private constructor
private GlobalSettings()
{
Console.WriteLine("constructor of GlobalSettings..");
}
 
// Exposes the only instance
public static GlobalSettings Instance()
{
return SingletonInstance;
}
// this is a sample setting value
public String SomeSetting()
{
return "I am Global Settings";
}
 
// Throws exception if any program tries to create another
// instance by cloning
public object Clone()
{
throw new ArgumentException("cannot clone this object");
}
} //end of class

To call a method on the singleton instance, a client program needs to use this expression. I have taken the example of the instance method called “SomeSetting”. Any instance method or property could be invoked in the same way as described below.

GlobalSettings.Instance().SomeSetting();

Please note that the class implements the ICloneable interface. So if another program tries to create another instance of this class by invoking code like shown below, they will get an exception.

GlobalSettings.Instance().Clone()

Hope you liked this post and video. Your feedback, as always, is valuable to me. Also, I’d like to hear from you how you implemented Singleton or the reasons why you do not use Singleton.

 

Leave a Reply

Your email address will not be published. Required fields are marked *