It seems all the object-oriented design guidelines say that if you want only one instance of a class to exist at any time, you should use a singleton. My question is, what are the advantages of using a singleton over simply using a class where all the member variables and methods are static?
Name:
Anonymous2008-05-07 20:24
In C++, you don't easily control the creation order of statics. It happens to initialize in whatever order the linker put them in. If they're initialized on first access like a singleton does, you can be sure that other stuff it depends on is initialized before it and you're not hitting segfaults before main().
In Java and other modern languages, there is no difference as the classloader initializes it in the proper order.
One other supposed advantage is that you can change the interface to a singleton much easier, or if you pass around the singleton then you can actually branch out to using multiple objects later on. However, since all singletons are access by Foo.instance() directly, you never bother passing them around anyway.