ASP.Net Singleton Design Pattern

In the previous blog, we have seen about “What is Design Pattern?” Now this is time to explore the types of Design Pattern.
Let’s start this with Singleton Design Pattern.
Singleton Design pattern belongs to Creational Design Pattern. Singleton Pattern ensures that only one instance of the class exists 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.

Let’s see this by an example:

Create a Console Application and create 4 classes Program.cs,  Singleton.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 avoids inheritance and so not even nested class can derive Singleton class to allow creating multiple instances.

If you like this blog please like us on Facebook:https://www.facebook.com/SharePointCafenet/

Prev Blog- What is design pattern?
Next Blog – Singleton Design pattern vs Static Class in C#

You may read some popular blogs on SharePointCafe.Net
 
You may read these articles -
Factory Design Pattern
SOLID Principles in C#

Leave a Comment

RSS
YouTube
YouTube
Instagram