Just the other day I have seen code that uses the so called singleton pattern. Meaning something along the lines of
class MySingleton{
public:
void foo() { ... }
static MySingleton&get_instance(){
static MySingleton singleton;
return singleton
}
private:
MySingleton(){ ... }
~MySingleton(){ ... }
int bar;
};
I do see why one would want to do that:
However I do not see why this way of doing things is superior to a couple of free functions. The way I'd implement it is to put
namespace some_name{
void foo();
}
in the header and
namespace some_name{
void foo(){
...
}
}
in the implementation file. If I need initialization and/or cleanup I either add a couple of functions that must be explicitly called or I add
namespace{
class Dummy{
Dummy(){ ... }
~Dummy(){ ... }
}dummy;
}
into the implementation file.
I know that this is from a semantic point of view a singleton, however I see the first variant used far more often in C++ Code than the second. Why? I consider the second version to be slightly superior, so I asking myself if I'm missing something obvious.
Can you please explain me why everybody uses the first variant? I don't see a single advantage over the good old way of doing things in C.
A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.
Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the Java Virtual Machine. The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, drivers objects, caching, and thread pool.
Use the Singleton pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program. The Singleton pattern disables all other means of creating objects of a class except for the special creation method.
According to the party line (E. Gamma, R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, Reading, MA, 1995, p. 128), the singleton offers the following advantages over the solution you propose.
Having said that, in most cases I consider the additional complexity excessive and rarely use the pattern in the code I write. But I can see its value when you design an API that others will use.
does this help?
What is so bad about singletons? http://steve.yegge.googlepages.com/singleton-considered-stupid
Rephrased: A singleton is a glorified global, so just 'implement' it as a global.
The construction of static MySingleton singleton;
gets called on the first use. (When get_instance()
is called.) If it is never called it never calls the constructor. Your method will call the constructor at static construction time. The previous method allows for the order and timing of the constructors being called. You method will order the construction of each singleton according to the compiler static initialisation order.
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