I have a project in which I have the following class tree with the main state machine class englobing everything. Shown is basically my classes to operate a TFT screen and a speaker.
|------- Sound ------- Speaker hardware IF
| ------ UI ------ Screen IF
Now, I want the user to be able to configure some settings that will show onscreen, notably sound volume. The sound volume variable must therefore be shared between the speaker hardware interface class and the screen interface class. It feels very heavy to do a series of setters and getters to move the variable up 2 levels to the state machine and then 2 levels down. Imagine with all the other settings that would come from different classes...
So I have 2 options in mind, but I am not sure that is the way a pro would do it. I was thinking about doing setters and getters but for a pointer to the private sound volume variable and bring it all the way up to the screen IF. this way, the screen would show the value straight from the class member value and edit it directly.
The other idea I had was to store another volume variable inside of the screen class, and then when it is changed, use a settingsChanged(int newVolumeLevel) callback function that would pass the volume value as argument and set the sound interface variable to the same value (that callback could be a pointer to a function belonging to the state machine, and that function when called from the pointer would just do something like speaker.setvolume(newVolumeLevel). Since in this case there are two volume variables, I am worried it may cause aliasing problems.
Anyway, I was wondering if any of you knew what is a common and robust way to modify private members in such convoluted class trees?
Thanks a alot for your help!
Perhaps create a class which would establish and maintain the invariants for sound volume, and with that we can expose the encapsulated sound volume as a public member object of, say, the speaker class.
although if the user changes the volume, the screen object still needs to tell the UI object, which will tell the state machine object, which will tell the sound_volume object via set/get? or you would use a pointer in the screen object that can directly change the volume memebr of the sound_volume object? Thanks!