Introduction:

This is one of the GOF patterns which is “Ensuring a class has only one instance and providing a global point of access to it”. Wherever and whenever we need a class which should be instantiated only once, Singleton comes in as a great choice. According to Dofactory Singleton is a frequently used pattern.

Using static classes or class which is full of static methods is an option as well. But such classes come in handy while giving a utility functionality like for example StringUtils. These classes do not need to maintain a state and we do not need to instantiate it many times wherever we need to use it. So keeping them static and instantiating them once is logical. But Singleton becomes important when we “require” a class to be a single instance. For example, I want to keep one single Logger instance or I want to keep one instance of reading and writing to a Serial Port.

A Simple implementation of Singleton class:

The following is an implementation which is most likely thread safe implementation with a double check and locking and no one can instantiate the class directly. One has to reach the object through DoubleCheckLockSingleton.Instance. Thread safety is one of the most important aspects wherever we have chance of multiple .net threads accessing/modifying an object.

    public sealed class DoubleCheckLockSingleton
    {
        private static volatile DoubleCheckLockSingleton _instance;
        private static object syncObj = new Object();

        private DoubleCheckLockSingleton() { }

        public static DoubleCheckLockSingleton Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (syncObj)
                    {
                        if (_instance == null)
                            _instance = new DoubleCheckLockSingleton();
                    }
                }

                return _instance;
            }
        }

        public void PrintTest()
        {
            Console.WriteLine("Hello from Double-check locking Singleton 4");
        }

    }

The above is a good method of implementing a singleton but even then we check for the object twice along with a lock. You can find more c# tutorials for reference.

A Fresh look of Singleton after .Net 4

.Net 4 has come up with a new System.Lazy type to make our lives easier writing code for lazy instantiation. Take a look at the following sample which passes a delegate to the constructor with a lambda expression.

    public sealed class LazySingleton4
    {
        private static readonly Lazy lazyObj = new 
            Lazy(() => new LazySingleton4());
        public static LazySingleton4 Instance { get { return lazyObj.Value; } }

        private LazySingleton4() { }
        public void PrintTest()
        {
            Console.WriteLine("Hello from Lazy Singleton 4");
        }
    }

As for now Singletons have gained importance and is being used in many cases like Singleton Loggers, UI Threads, using shared resources and even for Database connections. Hope you find the above snippets useful. We can also use IOC frameworks like Unity, Spring to give us Singleton classes instead of implementing our own singleton classes, but nevertheless we can use this wherever there is a need for custom implementation.