I’m building a C++ application, and I’ve got several utility objects that all of my classes need to use. These are things like the logging object, the global state object, the DAL object, etc…
Up until this point, I’ve been passing all of these objects around as references into my class constructors.
For example:
class Honda : public Car { public: Honda ( const GlobalState & state, const Log & logger, const DAL & dal ); ... private: const GlobalState & my_state; const Log & my_logger; const DAL & my_dal; }
This gets tedious fast, because each time I add a utility object that all of my classes need to access, I have to go and change the constructors everywhere.
I’ve heard that the correct way to solve this problem is to create one struct that contains all the different utility objects and pass that around (as a reference) to all of the objects that need to access it.
Is this the right way to handle this problem? Thanks!
UPDATE: Thank you to everyone for the feedback. After some additional research, I’ve decided to continue using Dependency Injection.
You could make use of the Service Locator pattern. This article introduces both dependency injection (which you are currently using) and service locator.
However, consider this: the idea of dependency injection is to have a system where each component has a well-defined responsibility and minimizes knowledge of other components where possible. If the component needs other components to do its job, then these are explicitly passed to it. This makes the component simpler to understand, more likely to be correct, and easier to maintain.
If you regularly need to add components which need to be known throughout the system, then there may be something wrong with the design of the system (or the way new features are being added to it). The dependency injection pattern just results in this problem being explicitly visible.