Reputation: 2927
What are the scenarios when Singleton design pattern is preferred over static class & when static class is preferred over Singleton design pattern?
Upvotes: 5
Views: 9454
Reputation: 13561
The main difference between the two is that a singleton can implement an interface, and that allows changing it's behavior for either testing or runtime reasons. A static class is static, and while it can have state, drastically changing it's behavior is going to be difficult.
A singleton, being an instance of a class, can implement an interface, and if used with methods that expect that interface, can easily be be replaced with different behavior.
Logging is a common usage for both.
A static logger is unlikely to be able to log to different medium (database, xlm file, text file, json, stream, webservice), because you either have to use a different API for the calls, or set some state and then have all of the methods impelement all of the different kinds of of persistence.
A singleton logger can implement ILog, and then if you need to switch from logging to a database to logging to a web service, you just use another class (which might or might not be a singleton).
A singleton can be used to move away from a static class slowly. A static class can replace a singleton when you realize that you are never going to be changing behavior.
Neither are hard to test in themselves. But it can be a problem to test other classes that use them. Particularly, a static class -- it is not unknown to want slightly different behavior in some respect (say logging, or having the source/destination of files be a file share vs sharepoint) when testing vs production. In that case, having the class be a singleton allows more easily changing that behavior.
Upvotes: 0
Reputation: 2927
My final take from this discussion: 1. An object has some state. State means current values of object's attributes. So, if you want to have a scenario, where you want to have some state that can be changed and also want to have only one instance, then use Singleton class. e.g. suppose there is a log file that you want to update after some successful operation or on some exception. To update this log file we must have a lock on it to avoid any inconsistent data, and it can be achieved through Singleton class. 2. When you do not require state of your object and want to load your class into the memory when application gets started & be remain there till life of application - use Static class.
Upvotes: 1
Reputation: 3417
Generally singletons are superior to static classes.
Singleton in contrary to static class:
If you choose static class then you choose concrete, there's no flexibility. However, if you use singleton you have to remember to make the instantiation of it thread safe.
Upvotes: 8
Reputation: 20320
This is not really an either or scenario.
Singletons are instances with a static getter, and a private constructor. They are not static classes.
Singleton with certain provisos is a way of ensuring you only have one instance of class.
So the first question is. Do you need an instance, i.e. does this thing have a state, the second question is given how difficult they make unit testing, do you want one at all.
Have a look at the Service Locator pattern, for instance.
Upvotes: 8
Reputation: 151
You can use a static class to offer you simple methods that do not require any state and when you do not need to instatiate an object.
Using a singleton means that you only want to instatiate an object once and you can pass it around and alter its state. With singletons you can also have inheritance or implement an interface.
Upvotes: 0
Reputation: 1244
Static class are specialty difficult to test. And you can't use the constructor for anything useful.
static classes are preferred in helper methods like the MVC helper.
You can see here tome limitations of a static class. They can only have static members and are sealed.
Upvotes: 2
Reputation: 2217
If you're only using a class as a container for some functions, use a static class.. but in most other cases, you're best off using the Singleton design pattern, because you'll probably want to reuse that object or instantiate it as a non-singleton.
Upvotes: 3