I have a requirement to have a only a single instance of a class at any given point of time. Singleton is the obvious candidate. But I have some other conditions that are not typical of a Singleton.
- The lifetime of the singleton is not the lifetime of the program. This object has to be created every time I enter a particular state and destroyed when I leave the state. For the entire duration of the state, I cannot create another instance of the class.
- Every time, I enter the state and create a new instance, I need to pass a variable to the singleton. Its a number, based on user selection.
So my implementation has the following static functions -
// To be called exactly once, everytime I enter the state
void MySingleton::CreateInstance(size_t count);
// To be called any no. of times during the state
MySingleton * MySingleton::GetInstance();
// To be called exactly once, when leaving the state.
void MySingleton::DestroyInstance();
Now this implementation is a major detour from conventional singleton implementation.
Is there any problem with such implementation?
Are there any better alternatives?