The best answers to the question “Difference between static class and singleton pattern?” in the category Dev.
What real (i.e. practical) difference exists between a static class and a singleton pattern?
Both can be invoked without instantiation, both provide only one “Instance” and neither of them is thread-safe. Is there any other difference?
The true answer is by Jon Skeet, on another forum here.
A singleton allows access to a single
created instance – that instance (or
rather, a reference to that instance)
can be passed as a parameter to other
methods, and treated as a normal
A static class allows only static
What makes you say that either a singleton or a static method isn’t thread-safe? Usually both should be implemented to be thread-safe.
The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that’s less common, in my experience), so you can pass around the singleton as if it were “just another” implementation.
The Singleton pattern has several advantages over static classes. First, a singleton can extend classes and implement interfaces, while a static class cannot (it can extend classes, but it does not inherit their instance members). A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded, leading to potential class loader issues. However the most important advantage, though, is that singletons can be handled polymorphically without forcing their users to assume that there is only one instance.
- Singleton objects are stored in Heap, but static objects are stored in stack.
- We can clone (if the designer did not disallow it) the singleton object, but we can not clone the static class object
- Singleton classes follow the OOP (object oriented principles), static classes do not.
- We can implement an
interfacewith a Singleton class, but a class’s static methods (or e.g. a C#
static class) cannot.