That's the standard form spray painted on the side of every highway overpass. You can get rid of the null test and add thread safety by initiating the single instance at the top:
But then we really ought to ask the next question (or maybe go back for the zero question.) Why is this a Singleton? What bad thing happens if two parts of the system make their own instances?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Feb 11, 2003
Thanks for the response and suggestion. Since the error messages do not change, there is no reason to have more than one instance. Since one instance --> Singleton.
Let me know if this approach is correct
Joined: Jan 29, 2003
I was asking if there's any reason not to have two or twenty -> don't bother with Singelton. The message collection is static which is enough to make it shared at the same scope as the Singleton.
Even though you could just use static methods, I like using an instance fine. And the getInstance() method is an opportunity to return a subclass some other day. Singleton or not becomes pretty unimportant after that.
Originally posted by John Lincoln: Since the error messages do not change, there is no reason to have more than one instance. Since one instance --> Singleton.
In my humble opinion, it needs a better reason to use a Singleton. A Singleton *might* make sense when you really, really, *really* want to *ENFORCE* (sorry for shouting) that there is always only one instance.
If you really don't care that much about whether there is one or more, I wouldn't use a Singleton, simply because that pattern also comes with a number of problems.
Taking a look at alternative patterns, such as Monostate or Just Create One might be a good idea, too.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus