Singleton Pattern vs. Static Class

Recently one of my friends asked me the difference between Singleton Pattern and Static Class. I said something really close, but I could not convinence him.  Later I Bing’ed it. I found the wide variety of difference. I consolidated some of them below

First let we see what is Singleton and Static Class over here.

Singleton Pattern: The name it self better describes about it.  Singleton means only once instance of the class. We can define it as a pattern that is used to restrict instantiation of that class to a single instance.

Below code describes the thread safe Singleton Pattern

 
public class SingletonPattern
    {
        private static SingletonPattern _instance = null;
        private static object _lock = new object();

        private SingletonPattern()
        {
        }
        public static SingletonPattern Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new SingletonPattern();
                        }
                    }
                }
                return _instance;
            }
        }

        public void DoSomthing()
        {
        }
    }

In above code SingletonPattern class contains the logic that always ensures that only on instance of the class is created. This is achieved by declaring a private constructor and a Static property of SingletonPattern class type.

The DoSomething method in above SingletonPattern class will be accessed in the code like below

 SingletonPattern.Instance.DoSomthing();

Static Class:  Static Class can be defined as a class that can’t be instantiated. This is achieved by placing a key word Static.  Static class will declared like below

public static class StaticClass

    {

        public static void DoSomthing()

        {

        }

    }

The DoSomething method in above StaticClass will be accessed in the code like below

 
StaticClass.DoSomthing();

I don’t know about you, but to me, there doesn’t appear to be a whole lot of practical difference here. In fact Static Class is much simpler than singleton pattern.

Now the real problem comes in to the picture, what if you want to extend or inherit new class from either of these? By default Static Class are sealed. You can’t extend Static Class. Where Singleton Pattern is take advantage over Static Class and few other differences between them are below.

  1. Singleton pattern will handle objects and subclass very well over Static Classes
  2. Singleton pattern will extend the classes and implement   the interfaces. Where Static Class is does not.
  3. we can easily “Lazy Initialization and asynchronous Initialization” the Singleton Classes
  4. Singleton Instance can be passed as parameter to other methods.

Hope now you are little clearer about Singleton vs. Static Class