Initializing a Program Using a Singleton
I have read several articles on why singles are bad.
I know it has few uses like logging, but what about initialization and deinitialization. Are there any problems with this?
I have a scripting engine that I need to link when running into a library.
Libraries don't have main (), so what should I use?
Regular functions or singleton.
Is it possible to somehow copy this object:
class
{
public:
static void initialize();
static void deinitialize();
} bootstrap;
If not, why do people hide the copy of the ctor, assignment operator, and ctor?
a source to share
C ++ libraries have a much easier way to do initialization and cleanup. This is exactly the same as you would for anything else. RAII.
Wrap whatever needs to be initialized in a class and initialize it in a constructor. Voila, problems are solved.
All the usual singlon problems still apply:
- You will need more than one instance, even if you didn't plan for it. If nothing else, you'll want it when you unit test it. Each test must initialize the library from scratch in order for it to work in a clean environment. This is difficult to do with the singleton approach.
- You are screwed as soon as these singlets start linking to each other. Since the actual initialization order is not displayed, you quickly end up with a bunch of circular references, accessing uninitialized singlons or stack overflows or deadlocks or other funny bugs that could get caught during compilation if you weren't obsessed with making everything global.
- Multithreading. It is generally a bad idea to force all threads to share the same instance of a class, because it forces that class to block and synchronize everything , which is high performance and can lead to deadlocks.
- Spaghetti code. You hide the dependencies of your code every time you use single color or global. It is no longer clear which objects the function depends on, because not all of them are visible as parameters. And since you don't need to add them as parameters, you can easily add many more dependencies than necessary. This is why singles games are nearly impossible to delete once you get them.
a source to share
The singleton goal is to only have one instance of a specific class on your system. C'tor, D'tor and CC'tor are hidden to have a single access point to receive only the existing instance.
Typically, an instance is static (can also be heap allocated) and private, as well as a static method (usually called GetInstance) that returns a reference to that instance.
The question you should ask yourself when deciding whether there should be one singleton is: do I really need to ensure that there is one object of that class?
There's also the issue of inheritance - this can complicate things if you plan on inheriting one single.
One more problem: How to kill a singleton (there are articles on the internet about this problem)
In some cases it is better to store your personal data statically rather than single, it all depends on the domain.
Note that if you are multi-threaded, static variables can give you XXX pain ...
So, you should carefully analyze your problem before deciding which design pattern you are going to use ...
In your case, I don't think you need a singleton because you want the libraries to be initialized at the beginning, but that has nothing to do with enforcing only one instance of your class. You can just put a static flag (static bool Initialized) if all you want is to ensure it is initialized only once.
Calling the method once is not enough to have one single.
a source to share
count the number of singletons in your design and call that number '
count the number of threads in your project and call this number 't'
raise t to the s-th power; that's roughly the amount of hair you are likely to lose by debugging the resulting code.
(I personally came across code that has 50+ singletons with 10 different races to get to .getInstance () first)
a source to share