I wasn’t really sure how to search for this question.
I’m doing an embedded system design with the following scenario.
-
I have a main application class that needs to create a bunch of hardware interfaces such as a keypad, display, communication ports, etc… a whole slew of stuff
-
Now I have all these objets in the main application that I can use which is great
-
The application class contains a few sub classes that it can go into and stay for a while. One example is a menu class that it enters and runs inside that class until the menu is closed
-
I need the menu class to also interact with a lot of a hardware objects that were created at the application level
-
What is the best way to go about this without using global variables? Is there a good solution to this problem?
I could pass each object into the menu class, but I don’t want to create a constructor with 20 arguments. My current solution is to put all the objects into a structure and pass that structure into the sub-class constructor. That way they also have access.
The part that bugs me about this approach is that I have to define the structure outside of the application which I don’t really like. Something just keeps telling me it’s not the best solution.
Open to any suggestions.
Presumably, there is ONE keypad – thus only one “Keypad Interface Object”, right? Similarly with Display [ok, there may be two displays, but still].
So my suggestion would be to have a registration and a “container” that holds the registered interfaces something like this:
Then you just have a Singleton Pattern to provide your HardwareRegistry, and register the devices as you create them during hardware initialization.
Of course, if you support different kinds of Keypads, Displays, etc, then you would implement those with a “interface baseclass”, and the registry returns the KeypadBase type, for example.