Saturday, May 5, 2018

C# : What is Singleton Design Patterns and How to implement in C#

What is Singleton Design Pattern?
  1. Ensures a class has only one instance and provides a global point of access to it.
  2. A singleton is a class that only allows a single instance of itself to be created, and usually gives simple access to that instance.
  3. Most commonly, singletons don't allow any parameters to be specified when creating the instance, since a second request of an instance with a different parameter could be problematic! (If the same instance should be accessed for all requests with the same parameter then the factory pattern is more appropriate.)
  4. There are various ways to implement the Singleton Pattern in C#. The following are the common characteristics of a Singleton Pattern.

    • A single constructor, that is private and parameterless.
    • The class is sealed.
    • A static variable that holds a reference to the single created instance, if any.
    • A public static means of getting the reference to the single created instance, creating one if necessary.
This is the example how to write the code with Singleton:
  1. namespace Singleton {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             Calculate.Instance.ValueOne = 10.5;  
  5.             Calculate.Instance.ValueTwo = 5.5;  
  6.             Console.WriteLine("Addition : " + Calculate.Instance.Addition());  
  7.             Console.WriteLine("Subtraction : " + Calculate.Instance.Subtraction());  
  8.             Console.WriteLine("Multiplication : " + Calculate.Instance.Multiplication());  
  9.             Console.WriteLine("Division : " + Calculate.Instance.Division());  
  10.   
  11.             Console.WriteLine("\n----------------------\n");  
  12.   
  13.             Calculate.Instance.ValueTwo = 10.5;  
  14.             Console.WriteLine("Addition : " + Calculate.Instance.Addition());  
  15.             Console.WriteLine("Subtraction : " + Calculate.Instance.Subtraction());  
  16.             Console.WriteLine("Multiplication : " + Calculate.Instance.Multiplication());  
  17.             Console.WriteLine("Division : " + Calculate.Instance.Division());  
  18.   
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22.   
  23.     public sealed class Calculate {  
  24.         private Calculate() {}  
  25.         private static Calculate instance = null;  
  26.         public static Calculate Instance {  
  27.             get {  
  28.                 if (instance == null) {  
  29.                     instance = new Calculate();  
  30.                 }  
  31.                 return instance;  
  32.             }  
  33.         }  
  34.   
  35.         public double ValueOne {  
  36.             get;  
  37.             set;  
  38.         }  
  39.         public double ValueTwo {  
  40.             get;  
  41.             set;  
  42.         }  
  43.   
  44.         public double Addition() {  
  45.             return ValueOne + ValueTwo;  
  46.         }  
  47.   
  48.         public double Subtraction() {  
  49.             return ValueOne - ValueTwo;  
  50.         }  
  51.   
  52.         public double Multiplication() {  
  53.             return ValueOne * ValueTwo;  
  54.         }  
  55.   
  56.         public double Division() {  
  57.             return ValueOne / ValueTwo;  
  58.         }  
  59.     }  

No comments:

Post a Comment