Let say we need to have just one instance of some class in our project. There are couple ways of doing it.
I want to compare. Please can you review my understanding.
1) Classical Singleton pattern
2) Completely static class (all methods and members are static).
As I understand the differences are following:
a) The order of initialization of static members across different units isn't defined. So, completely static members initialization can't use any static members/functions from other modules. And singleton doesn't have this problem.
b) We have to deal with threading for getInstance() of Singleton. However, completely static class doesn't have this problem.
c) Access to methods looks a little bit different. Foo::bar(); vs Foo::getInstance()->bar(); Generally, singleton can return NULL to identify that there were some problems with construction of object and static class can't.
d) Definition of class looks a little bit clunky with bunch of statics for static class.
Have I missed anything?
Whether you call it Singleton or Monostate or any fancy name... the very annoying nature of this is that you have ONE instance of the object and many writes to it: global variables, whatever their guise, are evil.
The idea that you need a unique instance is generally clunky. Most of the times what you really need is parts that communicate share the same instance. But another group of parts could perfectly use another instance without issue.
Any code that claim to need a global variable is highly suspicious. It may appear simpler to use one, but let's face it, you could perfectly pass the object to each and every function, it would complicate their signature but it would work nonetheless.
However, I admit, it appears simpler to use global variables... until you notice the issues:
Now, as far as singleton is concerned, multithreaded creation is not usable in C++ before C++0x (when it becomes possible using static locals), thus you need to create it in only one thread and delay access before: instantiate it in main, it's your best bet.
Destruction may cause mayhem since the life of the Singleton / Static may end before others are done with it, and then it's undefined behavior. This is typical of a Logger
singleton. The usual strategy is to shamelessly leak...
After that, if you still want one, I wish you good luck, that's all this community can do for you.
Another option you overlook is namespace
's.
namespace xyz { namespace { int private_variable; } int get_pv() { return private_variable; } }
Functionally, this is going to be similar to your option #2, but you can't accidentally "delete" this thing. You can't accidentally create an instance of it. It is just a collection of related globally accessible data and functions. You can (as in my example) even have "private" members and functions.
Of course the usage would be something like this:
int x = xyz::get_pv();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With