Wednesday 18 August 2010

Are we in right tone? The mistaken Singleton

Hiya... I think you might have bored about my first technical blog as the writing style was too bad. I am trying to recover from the lost presentation skill. Till then forgive me for the bad writing in the technical blogs...

When I was chit-chatting with my folks, somebody thrown a question what is the Singleton Class and the definitions were discussed and one folk came out with a word that the "Static" classes are singleton. Truly speaking the "Static" classes are not pure singleton class.

If we look close at the Static classes, the definition says it as static; for which you cannot instantiate the class. Whenever the class is defined as static, you cannot create a object. Instead you need to access the methods of the static classes by .. But for a singleton class you can create object. You can create constructors for the singleton classes but not in case of static classes.

So let us understand how we distinguish the Static from the Singleton. The Static class is defined like the following code:

Public Static Class myStaticClass
{
       public string myfirstMethod()
        {
                return "abcd";
        }

And when we are calling the method myfirstMethod in a class that needs to invoke this method we use the following syntax:

Public Class myMasterClass
{
         public static void main()
         {
              console.Write(myStaticClass.myfirstMethod());
         }

Here if we seen we didn't create an object. Instead, we are calling the method using the class directly to call the method instead of creating the object and call the method using the object.

But the definition of the Singleton class specified that you can create one and only one object for the singleton class and you need to destroy the object before creating another object for the singleton class. So you can use a "new" keyword to create an object for the singleton class against using he class name directly in the static classes. To achieve the singleton, the class uses "private" constructor against the "public" constructor for a class.

The following line of code explains the singleton class:

public sealed class mySingleton
{
    public static readonly mySingleton _instance = new mySingleton();

    ///
    /// Private constructor prevents instantiation from other classes
    ///

    private mySingleton() { }

   public static mySingleton Instance()
    {
        get
        {
            if (_instance==null)
            {
                _instance = new mySingleton();
            }
            return _instance;
        }
    }


      public string mysingletonMethod()
      {
            return "abcd";
      }

//Other Methods for this class.

}

Now in my main class when I am creating the object for this class, it will allow to create only one object. Please look at the following code:

Public Class myMasterClass
{
         public static void main()
         {
             mySingleton msObject=mySingleton.Instance();
             Console.Writeline(msObject.mysingletonMethod());
         }
}

So, this ensures that there is only one object created for the class. Folks, I think you will do it good at creating the Static Classes and the Singleton classes. No more confusions. Be a singleton hero.

Happy researching and coding!!!

Signed
The Techno Dork

2 comments:

  1. public static readonly mySingleton _instance = new mySingleton();

    Why we need this?
    And is it thread safe?

    ReplyDelete
  2. There is one issue in this line that it is marked as read only. As well this is not an thread safe code. I'll discuss the thread safe code for the singleton and the other pros and cons of the singleton...

    ReplyDelete