Question

Possible Duplicate:
Singleton: How should it be used

Following on from Ewan Makepeace 's excellent earlier question about the Singleton pattern, I thought I would ask "when does the Community believe that it is appropriate to use a Singleton?"

Let me offer up an example to critique:

I have an "IconManager" singleton. It begins by reading a properties file which indicates where my icons are located on disk, and then reads all the icons and caches them for future use.

The icons can be used all over my UI (tabs, tables, frames etc)... hence accessing them via a static Singleton method is very convinent. I also want to make sure that the icons are read once and only once (if would be very slow to read them from disk each time I needed one) Does the community believe this is an appropriate use of a Singleton? If not, how else might it have been implemented?

What other valid uses of Singletons might there be?

Was it helpful?

Solution

Your IconManager implements the factory pattern, it builds icons. And you probably only need one factory to build icons. So no problems for this case to use a singleton IMHO. I've built software with several of these centralized factories and everything worked out fine.

See also this thread: Most common examples of misuse of singleton class

OTHER TIPS

An alternative approach would be to create an instance of your class that loads up the icons and then you pass a reference to this instance to each and every control that needs to access the resources. That way in the future you could have more than one icon loader and pass them around as needed. More flexible for the future but with the rather large downside of making you pass the reference around to a zillion controls.

A good use of the singleton is when accessing a resource that may only have one active connection. There are many hardware devices that have this limitation.

Let's say you are connecting to a CCTV camera that only allows one connection. The Singleton pattern would create this connection on first use and keep it open. Whenever you needed a picture from the camera, possibly from multiple sources, you can hit the Singleton knowing that, al other problems considered, the picture will be available.

If the camera has a slow initial connection time too, holding the connection open in this way rather than opening the connection, grabbing a picture and closing the connection again could be much more efficient.

I have actually never used a singleton, but haven't used design patterns much. I think that they ae very valuable when other patterns call for them like the Factory and Gateway patterns. However, they almost never are good all by themselves.

You might want to consider the Monostate Pattern which gives you all of the benefits of the singleton without many of the drawbacks. This also allows you to have a rich object that has state that just happens to have the global properties that you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top