Design Pattern Tutorials

Static Class vs Singleton

Differences between Singleton and static classes:

  • Static is a keyword and Singleton is a design pattern
  • Static classes can contain only static members
  • Singleton object is stored on heap
  • Singleton objects can be cloned
  • Singleton object can be passed as a reference
  • Singleton supports object disposal
  • Singleton is an object creational pattern with one instance of the class
  • Singleton can implement interfaces, inherit from other classes and it aligns with the OOPS concepts

Static class example - Temperature Converter : We are pretty sure that the formulas for foreign heat to Celsius conversion and vice versa will not change at all and hence we can use static classes with static methods that does the conversion for us. Please refer to the below code for more details.

Real world usage of Singleton : Listed are few real world scenarios for singleton usage

  • Exception/Information logging
  • Connection pool management
  • File management
  • Device management such as printer spooling
  • Application Configuration management
  • Cache management
  • And Session based shopping cart are some of the real world usage of singleton design pattern

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StaticDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            double celcius = 37; double fahrenheit = 98.6;
            Console.WriteLine("Value of {0} celcius to fahrenheit is {1}",
                celcius, Converter.ToFahrenheit(celcius));
            Console.WriteLine("Value of {0} fahrenheit to celcius is {1}",
                fahrenheit, Converter.ToCelcius(fahrenheit));
            Console.ReadLine();
        }
    }
}


Converter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StaticDemo
{
    public static class Converter
    {
        public static double ToFahrenheit(double celcius)
        {
            return (celcius * 9 / 5) + 32;
        }
        public static double ToCelcius(double fahrenheit)
        {
            return (fahrenheit - 32) * 5 / 9;
        }
    }
}

;