Not having much formal training in programming methods or patterns, my first introduction to the singleton pattern was while working at the startup-that-got-swallowed-by-HP where a junior programmer was given a 5 minute overview of it and sent on his way to go implement it. I thought it was kinda cool, and went on with what I was doing.

Fast forward a number of years and a couple singleton links have crossed my desk. The first is Singletons Done Safely which argues that singletons are not necessarily bad if you use them with discipline. The examples Jason uses are in C#, but should be portable to other languages.

Of course, this defense would not be necessary if there was not the larger belief that seems to be growing that singletons are evil. Why Singletons Are Controversial covers the 3 main arguments against their use. In fact, the article is written by the same person who wrote the Google Singleton Detector; a static analysis tool that detects four different types of global state, including singletons, hingletons, mingletons and fingletons.

Given the controversial nature of *ingletons, if you are a java shop I would recommend you run GSD against your code base to see if you have any instances of this pattern in your code. If so, some careful peer review should happen to ensure that the pattern is implemented correctly. I would not incorporate it into the CI build though as these things likely don’t crop up too often. Once you are sure you code is okay, a development policy of making sure singletons are well vetted before being checked-in should be enough to provide a safety net.