mardi 15 mars 2016

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 class clsSingleTon
{
   public static int intcounter;
   private clsSingleTon()
   {
   }
   public static void Hit()
   {
   }

  public static int getTotalHits()
  {
   return intCouner
  }
}

and in client

clsSingleTon.Hit();
MessageBox.Show(clsSingleTon.getTotalHits().ToString());

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?

Aucun commentaire:

Enregistrer un commentaire