mocking a singleton

Jeffrey Frederick (link via Robert) highlights a splendid piece of humour by Mike Hill on the Yahoo testdrivendevelopment list. In discussing the use of mock objects to test Singletons, Mike said:

“Mocking a singleton is relatively easy.”To begin with, singleton’s are all alone in the world; don’t be afraid to point this out. Also, most singletons are created, get this, not even in a factory, but merely in a factory method. Finally there is the point that they are little more than dressed-up globals, and IME experience cause more heartache than they’re worth. You can point to the many nasty screeds about singletons there are out on the net. All in all, mocking it shouldn’t be too hard. A word: if your singleton has unusually large body parts, stoop to insulting that. Sometimes, to be kind, you have to be cruel.”

The humour arises mostly from the pun on ‘mock’, of course, but also partly from deconstruction of the term ‘singleton’. I find it fascinating just how quickly and completely a metaphor such as this can come to be adopted as primary language. To the extent that we get a small shock when someone reminds us where the term originally came from.

boot Many of the GoF design patterns have names that are metaphors (Bridge, Factory, Singleton, etc) and I think that’s an important factor in their success. A few lines of code in some software aren’t really a Factory; but the name evokes so many connections with the real world counterpart that we can quickly understand what those lines of code do (or were intended to do). We’ve stolen a word form another domain, so that the baggage it brings along will help us understand something new and more abstract.

Our industry is riddled with metaphors: windows, mouse, boot-strap, object, broker, layers. They help us bring our real-world, tangible experiences to bear on complex, abstract subjects. It would be hard to imagine life without them.

Look out for my conference workshops on the power and use of metaphors later this year. In the meantime, what has been the most useful metaphor you’ve encountered in software development? Which metaphors are the most surprising? Which are the most deeply ingrained?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s