Waldemar's Blog

Singleton Pattern

The Singleton Pattern is something many people have used without even realising it. It's used in software engineering to restrict the instantiation of a class to one object. In other words, it ensures that a class has only one instance and provides a global point of access to that instance.

In other...other words it's useful in situations where you need to ensure that only one instance of a class exists and that it is easily accessible to other parts of your code.

A good example would be a logging system that needs to be accessed by multiple components. You use a singleton to sure that there is only one instance of the logging system, and that all components access the same instance.

Here's a simple example of how to implement the Singleton Pattern in C#:

public class Singleton
{
    private static Singleton instance;
    
    private Singleton()
    {
        // Private constructor to prevent object instantiation from outside the class
    }
    
    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}

Here we see Singleton has a private constructor to prevent object instantiation from outside the class. Instead, we use the Instance property to access the class:

Singleton instance = Singleton.Instance;

That's as simple as it gets!

This is all fine and well if we're in a single-threaded environment. If we move to multiple threads this class might not be very safe to use. I'm not going to address thread safety in this article but a simple way to make this singleton thread safe would be as follows:

public sealed class Singleton
{
    private static volatile Singleton instance;
    private static object syncRoot = new object();

    private Singleton() { }

    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
}

Here we used the double-check locking technique and the Singleton classic is made thread-safe by using the lock statement to ensure that only one threat at a time can create the instance of the class.

The volatile keyword tells the compiler to exclude the property from various optimisations and ultimately ensures the value of instance is always up-to-date across different threads.

The double-check locking technique is used to minimise the performance impact of the lock statement. We can implement the class similarly using something like semaphores.

A final note is that this implementation is lazy-loaded. In other words, the Singleton is not instantiated until it is requested. You could easily create it eagerly by initialising the instance field when it is declared.

#coding #designPatterns