Monday, May 12, 2003 04:52 PM
There are two questions to ask before deciding to use a singleton:
- Is it absolutely necessary to control the number of instances of this class? Is there any problem with having multiple instances of this class? Will there be resource contention or deadlocks if I don't control the number of instances?
- Do I need to make this class globally accessible?
In your case the second is probably true. Whether the first question is true or not is for you to decide. If that is not scrictly true, you could just instantiate and keep a globally accessible object.
The advantage is, if this object gets used in many places, the calling code will look like
Singleton is a simple pattern to use and hence is also easy to misuse. In my opinion, the answer to the first question is the key. If having multiple instances of the class will not cause any harm, just use a regular class with static methods.