Software

Singleton

The singleton pattern refers to having only one object of a class. Again, enforces one and only one object of a Singleton class which globally accessible within a program.

Code example:

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

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

         return instance;
      }
   }
}
Singleton Class Diagram.png
Singleton class diagram.

One thought on “Singleton

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.