Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the need of private constructor in singleton design pattern?

when i go through the below code, i couldnt find the reason why it using private constructor in the sample?

public sealed class Singleton
    {
        private static Singleton instance = null;
        private Singleton()
        {
        }

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

                return instance;
            }
        }
    } 

...

  //Why shouldnt I use something like below.
  public class Singleton
  {
       private static Singleton instance = null;            

       static Singleton()
       {
       }

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

                return instance;
            }
        }
    } 

instead of public class if i created a static class, i can use the class directly rather than creating instance. what is the need of creating a private constructor here, when the static keyword persists to the same job?

any other advantage for following this pattern?

like image 209
Tom Cruise Avatar asked Feb 07 '23 12:02

Tom Cruise


2 Answers

A singleton class and a static class are different things, and it seems you're mixing that up. A static class has only static methods and static members and therefore cannot have a constructor. Static methods are called on the type, not the instance.

A singleton class, in contrast, has normal methods and is called using an instance. The private constructor is there to prevent creating multiple instances of the class and usually used by a private property that returns this only instance.

public class Singleton
{ 
    static Singleton s_myInstance = null;
    private Singleton()
    {
    }

    // Very simplistic implementation (not thread safe, not disposable, etc)
    public Singleton Instance 
    {
        get 
        { 
             if (s_myInstance == null) 
                   s_myInstance = new Singleton();
             return s_myInstance;
        }
     }
     // More ordinary members here. 
}

The advantage of singletons is that they can implement interfaces. Also, they should be preferred over static classes if they are stateful (have many members), as having many static fields in a static class is quite ugly design.

like image 137
PMF Avatar answered Feb 10 '23 02:02

PMF


Since Singleton can have one instance only, you have to prevent second instance creation. If you skip constructor declaration, e.g.

  public class clsSingleTon {
  }

one can call a default constructor:

  var one = new clsSingleTon();
  var two = new clsSingleTon(); // <- that's we try to ban

if you declare constructor being public, one can call it, so the only option is private one:

  public class clsSingleTon {
     public static int intcounter;

     // No-one will call it
     private clsSingleTon() {
     } 
     ...
  }

however, it seems that you don't want any instances at all, so drop the constructor and declare the class as static:

  // Static class - no instances are allowed 
  public static class clsSingleTon {
    //TODO: turn it into property
    public static int intcounter;

    public static void Hit() {
    }

    //TODO: turn it into property, do not mimic Java
    public static int getTotalHits() {
      return intCouner;
    }
  }

  ...

  clsSingleTon.Hit();
  MessageBox.Show(clsSingleTon.getTotalHits().ToString());
like image 33
Dmitry Bychenko Avatar answered Feb 10 '23 01:02

Dmitry Bychenko