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?
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?
When I want class with full functionality, e.g. there are many methods and variables, I use singleton;
If I want class with only one or two methods in it, e.g. MailService class, which has only 1 method SendMail() I use static class and method.
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.
In an article I wrote I have described my point of view about why the singleton is much better than a static class:
We can create the object of singleton class and pass it to method.
Singleton class doesn't any restriction of inheritance.
We can't dispose the objects of a static class but can singleton class.
Singleton's are instantiated, it's just there's only one instance ever instantiated, hence the single in Singleton.
A static class can't be instantiated by anything other than itself.
Main differences are: