ASP.Net Singleton Design Pattern

Singleton Design pattern belongs to Creational Design Pattern. Singleton Pattern ensures that only one instance of the class exist through out the application life cycle.

Singleton design pattern is the best known pattern in software development.
So, if you are following singleton design pattern then you can not create multiple instances of a single class.

Lets see this by an example:

Create a Console Application and create 4 classes Program.cs,  Sinleton.cs , Product.cs, Employee.cs

Singleton.cs

namespace DesignPatternPractice
{
  public sealed class Singleton
    {
        private static int counter = 0;
        private static Singleton objSingleton = null;
        public static Singleton GetObject
        {
            get
            {
                if (objSingleton == null)
                    objSingleton = new Singleton();
                return objSingleton;
            }
        }

        private Singleton()
        {
            counter++;
            Console.WriteLine("Object created. Instance - " + counter);
        }

        /// <summary>
        /// Function to get Product Name
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public string GetProductData(int productId)
        {
            string productName = string.Empty;
            // Some logic
            productName = "XYZ Product";
            return productName;

        }

        /// <summary>
        /// Function to get Employee Name
        /// </summary>
        /// <param name="employeeId"></param>
        /// <returns></returns>
        public string GetEmployeeData(int employeeId)
        {
            string employeeName = string.Empty;
            // Some logic
            employeeName = "Ram Kumar";
            return employeeName;
        }
    }
}



Employee.cs

namespace DesignPatternPractice
{

    class Employee
    {
        //Singleton objSingleton = new Singleton();//Will throw error
        Singleton objSingleton = Singleton.GetObject; //No object created
        public void GetData()
        {
           Console.WriteLine(objSingleton.GetEmployeeData(1));
        }
       
    }
}



Product.cs

namespace DesignPatternPractice
{
    class Product
    {
        //Singleton objSinleton = new Singleton(); //Will throw error
        Singleton objSinleton = Singleton.GetObject; 
        public void GetData()
        {
           Console.WriteLine(objSinleton.GetProductData(12));
        }
    }
}


Program.cs (With Main Function)

namespace DesignPatternPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee objE = new Employee();
            objE.GetData();
            Product objP = new Product();
            objP.GetData();
        }
    }
}

Once you run this application you can see below output screen.
In Product.cs and in Employee.cs we have called functions from Singleton.cs but with only a single instance.




Now there may be a query that why Singleton class is sealed. The answer is sealed class avoid inheritance and so not even nested class can derive Singleton class to allow creating multiple instances.

Comments

Popular posts from this blog

CAML Query in SharePoint 2010

Calling REST APi from server side code - C#

All about SharePoint 2010 Content Type Hub

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX