Suggested Videos
Part 3 - Why is singleton class sealed - Text - Slides
Part 4 - Thread Safety in Singleton - Text - Slides
Part 5 - Lazy vs Eager loading in Singleton - Text - Slides
Differences between Singleton and static classes
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
Part 3 - Why is singleton class sealed - Text - Slides
Part 4 - Thread Safety in Singleton - Text - Slides
Part 5 - Lazy vs Eager loading in Singleton - Text - Slides
Differences between Singleton and static classes
- Static is a keyword and Singleton is a design pattern
- Static classes can contain only static members
- 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
- Singleton object can be passed as a reference
- Singleton supports object disposal
- Singleton object is stored on heap
- Singleton objects can be cloned
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;
}
}
}
I have one doubt related to point 4 "Singleton can implement interfaces, inherit from other classes and it aligns with the OOPS concepts". In Singleton Pattern we use Sealed class then how this can be inherited ?
ReplyDeleteHi it is the other way round. Singleton can inherit from lnterfaces and classes. No class can inherit from singleton class
Delete