I have 3 interface classes IVideo , IAudio , IGPIO and three other classes that will implement those interface: Video_impl , Audio_impl , GPIO_impl.
Things is simple so far.
But then ,I want all those object to be singleton. Here are the questions:
Is it a good idea to abstract an Interface ISingleton , so that Video_impl , Audio_impl , GPIO_impl (or IVideo , IAudio , IGPIO ?) can inherit from it?
I was thinking of implement it in following way. Is it recommended? I think there must be better ways.
//Isingleton.hpp
template <class T>
class ISingleton
{
public:
virtual T *getInstance() = 0;
};
class IGPIO
{
public:
virtual int SelectAudioInput() = 0;
};
class GPIO_impl : public IGPIO, public ISingleton<IGPIO>
{
public:
IGPIO *getInstance();
int SelectAudioInput() ;
private:
IGPIO *instance;
};
I would recommend reading Alexandrescu’s “Modern C++ Design”. In it, among many other things, he designs a fully-fledged singleton template and thinks through many of the issues, such as when it should be destroyed, whether it should resurrect after being destroyed because it is needed during the destruction of other singletons, and all that good stuff.