I have some singletons in my program. For example an App singleton (controls game loop etc) and a Logger and MemoryManager singleton. I guess it is preferable to control the lifetime of the singletons, for example I want to be sure logging in the App destructor is possible. Therefore I thought about this design: The App singleton is kinda like a singleton manager. App is created first, creates all other singletons and is also responsible for destroying them.
The code would roughly look like this:
// Main Singleton
class App {
private:
App();
App(const App& rhs);
~App();
App& operator=(const App& rhs);
static App* instance;
public:
static App* createInstance() {
if(instance == NULL)
instance = new App();
return instance;
}
static App* getInstance() {
assert(instance);
return instance;
}
static void destroyInstance() {
if(instance)
delete instance;
}
private:
App() {
// Andere Singletons erzeugen:
Logger::createInstance("MyLogger");
MemoryManager::createInstance();
...
}
~App() {
// Andere Singletons zerstören:
Logger::destroyInstance();
MemoryManager::destroyInstance();
...
};
//Usage:
int main() {
App* a = App::createInstance();
// Now App and all other singletons are usable
a->foo();
...
App::destroyInstance();
}
Do you think this will work? Or do you see any big problems/design flaws?
Do you think the design is good?
PS: I am NOT using multithreading.