Friday, December 16, 2005

Singleton design pattern

Mohnish & I had a small discussion recently about Singleton vs static method only class.

A few advantages of Singleton we came up with were..

Singletons can be subclassed. And preferably used with Factories or Registries.

Another subtle difference was usage. When you have a singleton, you get back a class instance on which you then perform operations. Like..

Singleton singleton = Singleton.getInstance();

Compare this to ..


The Singleton is just more correct because it allows to think in terms of a type and its behaviour and limits the instances. The static method form does not exhibit the same level of coherent behaviour. It's not the most cleanest distinction but I guess you know what I mean.

I was in the mood to read more on this and stumbled on an article on Javaworld - Simply Singleton. Definitely worth a read.

...which reminded me of a basic feature of Singletons we missed - controlled instance creation

No comments: