Design Patterns in the Test of Time: Singleton
In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.
I won’t show code or diagrams here. If you don’t know the Singleton pattern, you probably don’t have any business reading this series. Go hit the books and then come back for the rest of my review.
Of the top of my head, I can’t think of a single pattern that has been as denigrated as the Singleton pattern. It has been the bane of testers anywhere, and just about any Singleton implementation had had to become thread safe, given the demands that we usually have from our apps.
That basically means that any time that you use a Singleton, you have to be damn sure that your code is thread safe. When it isn’t, this becomes really painful. That along would be a huge mark against it, since multi thread proofing code is hard. But Singleton also got a bad rep because they create hidden dependencies that were hard to break. Probably the most famous of them was HttpContext.Current and DateTime.Now.
Singleton may have a tattered reputation and wounded dignity, but it is still a crucially important pattern. Most of the issues that people have with the Singleton aren’t with the notion of the single instance, but with the notion of a global static gateway, which means that it becomes very hard to modify for things like tests, and it is easy to create code that is very brittle in its dependencies on its environment.
Common workaround to that is to break apart the notion of accessing the value and the single nature of the value. So you typically inject the value in, and something else, usually the container, is in charge of managing the lifetime of the object.
Common use cases for Singletons include caches (which would be pretty bad if they didn’t stuck around) and the NHibernate’s Session Factory (which is very expensive to create).
Recommendation: The notion of having just a single instance of an object is still very important, especially when you use that single instance to coordinate things. It does means that you have multi threading issues, but that can be solved. It is a very useful pattern, but you have to watch for the pit falls (global static accessor that is used all over the place is one of the major ones).
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Pietro Martinelli replied on Tue, 2012/11/20 - 7:41am
You can solve the contrast between Singleton's pros and cons by distinguishing the functional meaning of being a "singleton" and the syntactic one: I've discussed this approch in my blog post Singleton, testing and dependency inversion, some time ago.
David Whatever replied on Wed, 2012/11/21 - 1:45am
Many of the singleton problems are solved by instead using Dependency Injection. Rather than making a singleton a global, you expose the object into others who have a dependency on it. This way, you can both track where the object is to understand and reduce the coupling on the singleton, and can easily replace the singleton with another instance, for instance in testing scenarios.