Global singletons are evil

I’ve read many good blogs recently detailing why global singletons are evil. This thought has been brewing in me a long time. No design pattern is easier for the novice to pick up and use and misuse as the singleton. The (mis)perceived elegance of hiding global variables in a singleton is truly an evil thing. Sure Singletons are better than have all static access methods, and sure having all static access methods is better than having global variables and routines for the same thing. But the lesser of two evils (or three) is still evil. The desire to constrain a class to one instance is a good one when appropriate. I prefer the thought that singletons should return a valid object pointer on the first call and null on subsequent calls. This is what you would expect a class that can only have one to do. Using this method you get one instance that you use in the same manner as normal class instances. There should be a support group for people who like to hide globals in singleton’s – for all their handwaiving they aren’t fooling anyone;)

I have seen project where just about every class is a global singleton ….. how very (un)object oriented. Even Vlisside’s jumps on the band wagon in Pattern Hatching bringing up his regrets about this pattern. One simple change can clean it up so nicely – only return the valid instance ONCE.

This entry was posted in General. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>