Jens Schauder is software developer since 1997. He loves software development for the constant challenges and constantly changing environment. A great chance to learn and teach. He is also blogger, author of various articles and speaker at conferences. Jens is a DZone MVB and is not an employee of DZone and has posted 87 posts at DZone. You can read more from them at their website. View Full User Profile

Fixing the Singleton

  • submit to reddit

When you ask people who know almost, but not completely nothing about patterns, about software design patterns, they probably bring up the singleton pattern. It’s extremely easy: Just make sure you have one single instance of a class.

If you ask more experienced developers about the singleton, they will probably tell you its bad and should be avoided.

What is so bad about the singleton that it fell from the heavon of patterns into the hell of anti patterns?

If you check the description of the singleton pattern in the Gang of Four book it talks about two characteristics of the singleton:

  • There can be only  a single instance
  • There is a central access point to that instance

And there really is nothing wrong about the first point. Sometimes you really can’t have more then one instance, but even if multiple instances don’t hurt, if they don’t give you any benefit (i.e. if they are stateless) you might as well reuse a single instance.

The problem is really with the central access point. This makes the singleton look and behave like a global variable. And how should we obtain our dependencies? Correct through Dependency injection.

So the next time you think: “Looks like a singleton to me”. Go ahead, create a singleton. But also create an interface which gets implemented by the singleton and make it very clear that nobody may use this singleton except via its interface and dependency injection through constructor.



Published at DZone with permission of Jens Schauder, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Sandeep Bhandari replied on Sun, 2012/01/29 - 12:12pm

Good tip there. By using the interface, we definitely have more control over singleton. Ways to Create Thread Safe Singleton

Steven Baker replied on Sun, 2012/01/29 - 9:24pm

I'm not sure if I agree with the interface part, but if you do put an interface in front of your singleton, I hope you make sure your singleton is threadsafe.

Senthil Balakrishnan replied on Mon, 2012/01/30 - 10:33am

Singleton by far is the highly used & most misused pattern of all. I am a bit lost on the interface part of it, reason being I have never came across a situation where a singleton exhibits polymorphic behavior. Generally they are very unique & don't fit into polymorphism. One good reason I can think of is, when unit testing injecting a mock would be piece of cake.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.