| Difference between Static vs Singleton class
Below are the most common differences between Static and Singleton class.
Static Class
(1)As from last couple of questions we knows that we cannot create an instance of static class.
(2)When compiler compiles the static class it treats static class as an abstract and sealed class. So we cannot create instance for static
class and nor extend that.
(3)No Instance we can create
(4)We cannot pass the static class as method parameter.
(5)Static class is generally initialized when it is first loaded for the first time and it may lead to potential classloader issues.
(6)These are not possible with a static class. So the Singleton class is more flexible as compared to static classes.
(7)We will not create clone of static class
(8)It is not possible to dispose the static class.
(9)It is not possible to implement DI design Pattern using Ststic class because ststaic class is not interface driven.
(10)Static class does not have any Object pointer, so the scope is at the App Domain level.
Singleton Class
(1)We can only create a single instance of Singleton class and later on we can reuse that singleton instance.
(2)We cann create a instance.
(3)One important aspect is that Singleton class constructor is always marked as private. So for private we cannot create instance from outside the singleton class. It provides either public static property or a public static method whose job is to create the singleton instance only once and then return that singleton instance each and every time when we called that public static property/method from outside the singleton class.
(4)And we can pass singleton instance as a method parameter in c#.
(5)A Singleton class can be initialized lazily or can be loaded automatically by CLR when the program or namespace containing the Singleton class is loaded.
(6)It is possible to implement interfaces, inherit from other classes and allow inheritance with Singleton class.
(7)It is possible to clone the Singleton class object
(8)We can dispose the objects of Singleton Class
(9)We can implement Dependency Injection in Singleton Class.
(10)In Singleton Class a single object across the application lifecycle, so the scope is at the application level. | | |