So I'm rather new at OO Design, and I'm wondering about the use of the Singleton design pattern. I've read some articles about why singletons are bad but still can't figure out if I could need one or not. I'd like to avoid it as much as possible.
In my case, I work with OceanOptics Spectrometers, that can be controlled and consulted through their API, in C++.
I've put all the code that manages spectrometers (discovering them, setting or getting parameters, retrieving data) in a single class SpectrometerProxy
.
And I would like to know if this class should be a singleton or not. I feel that there might be a few reasons that could justify it:
It manages hardware
Whatever the number of spectrometers, they're all controlled and consulted through this class
There's a specific procedure that must be done in a precise order, and only once (opening spectrometers, checking some variables, and closing spectrometers when the program stops)
Then, I don't know if there are better ways of implementing this class than making it a singleton. The other solution I thought of was keeping it as a normal class but prevent copying (by declaring copy constructor and assignment operator private) and just passing a pointer to the classes that need it : it would not prevent the creation of multiple SpectrometerProxy
and I'd like to avoid that.
I also thought of making it all static, but then it would rely on the client code to call the correct static member functions in the correct order (and not forgetting to close correctly the connections to spectrometers), and thus would be error-prone and contrary to the RAII principle.
So, would a singleton be a possible correct design approach for this problem, or should I exclude it and search for other ways ?