What are Singleton Design Patterns and how to implement them in C#?

Category: C SharpWhat are Singleton Design Patterns and how to implement them in C#?
Editor">Editor Staff asked 2 years ago

What are Singleton Design Patterns and how to implement them in C#?

2 Answers
Editor">Editor Staff answered 2 years ago

What is a 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 the 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.

Example of how to write 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.             Console.WriteLine(“\n———————-\n”);  
  11.             Calculate.Instance.ValueTwo = 10.5;  
  12.             Console.WriteLine(“Addition : “ + Calculate.Instance.Addition());  
  13.             Console.WriteLine(“Subtraction : “ + Calculate.Instance.Subtraction());  
  14.             Console.WriteLine(“Multiplication : “ + Calculate.Instance.Multiplication());  
  15.             Console.WriteLine(“Division : “ + Calculate.Instance.Division());  
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19.     public sealed class Calculate {  
  20.         private Calculate() {}  
  21.         private static Calculate instance = null;  
  22.         public static Calculate Instance {  
  23.             get {  
  24.                 if (instance == null) {  
  25.                     instance = new Calculate();  
  26.                 }  
  27.                 return instance;  
  28.             }  
  29.         }  
  30.         public double ValueOne {  
  31.             get;  
  32.             set;  
  33.         }  
  34.         public double ValueTwo {  
  35.             get;  
  36.             set;  
  37.         }  
  38.         public double Addition() {  
  39.             return ValueOne + ValueTwo;  
  40.         }  
  41.         public double Subtraction() {  
  42.             return ValueOne – ValueTwo;  
  43.         }  
  44.         public double Multiplication() {  
  45.             return ValueOne * ValueTwo;  
  46.         }  
  47.         public double Division() {  
  48.             return ValueOne / ValueTwo;  
  49.         }  
  50.     }  
  51. }  
Editor">Editor Staff answered 2 years ago

The basic difference is that the Throw exception overwrites the stack trace. This makes it hard to find the original code line number that has thrown the exception.

 
Throw basically retains the stack information and adds to the stack information in the exception that it is thrown.
 
Let’s see what it means to better understand the differences. I am using a console application to easily test and see how the usage of the two differ in their functionality.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace TestingThrowExceptions {  
  6.     class Program {  
  7.         public void ExceptionMethod() {  
  8.             throw new Exception(“Original Exception occurred in ExceptionMethod”);  
  9.         }  
  10.         static void Main(string[] args) {  
  11.             Program p = new Program();  
  12.             try {  
  13.                 p.ExceptionMethod();  
  14.             } catch (Exception ex) {  
  15.                 throw ex;  
  16.             }  
  17.         }  
  18.     }  
  19. }  

Now run the code by pressing the F5 key and see what happens. It returns an exception and look at the stack trace.