c++ - Should I use a singleton for hardware management ? -
so i'm rather new @ oo design, , i'm wondering use of singleton design pattern. i've read articles why singletons bad still can't figure out if need 1 or not. i'd avoid as possible.
in case, work oceanoptics spectrometers, can controlled , consulted through api, in c++.
i've put code manages spectrometers (discovering them, setting or getting parameters, retrieving data) in single class spectrometerproxy
.
and know if class should singleton or not. feel there might few reasons justify it:
it manages hardware
whatever number of spectrometers, they're controlled , consulted through class
there's specific procedure must done in precise order, , once (opening spectrometers, checking variables, , closing spectrometers when program stops)
then, don't know if there better ways of implementing class making singleton. other solution thought of keeping normal class prevent copying (by declaring copy constructor , assignment operator private) , passing pointer classes need : not prevent creation of multiple spectrometerproxy
, i'd avoid that.
i thought of making static, rely on client code call correct static member functions in correct order (and not forgetting close correctly connections spectrometers), , error-prone , contrary raii principle.
so, singleton possible correct design approach problem, or should exclude , search other ways ?
it's religious theme: use or not use singletones. according experience working hardware (10+ years) i'd prefere not use them. first: as murphy's law says tomorrow you'll need work more 1 spectrometers. second: it's better make virtual abstract interface methods need interact hardware, inherit , write code, actualy deals hardware. way useful when not have access hardware need debug app. in case subclassing interface , making emulating functions (or better functions, read actual data recorded file data, got hardware), rest of program stay unchanged.
Comments
Post a Comment