Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid using a global for the data behind a GUI?

I am writing an audio processing standalone application. I have an AudioManager object that wraps things to do with the engine (such as I/O device management, signal processing routing, running state). I am writing a GUI to control the AudioManager running in the background. Currently, every component that needs to message the AudioManager needs a pointer to it.

This starts to get crazy when a deeply nested object needs a pointer to the AudioManager, as it means that I need to pass the pointer through the constructors of GUI objects that don't directly care about AudioManager (only some subcomponents need to know).

I could just make AudioManager a singleton to avoid the boilerplate, but the information flow from the class is bidirectional, so this is probably a bad idea. I does also feel a little fishy wrapping everything into a big class, but it makes it a little easier to pass about. Is there a common pattern to avoid the mindless pointer passing?

Below is a bit of pseudocode showing some constructors highlighting the basic type of problem. I've tagged this C++11 to see if that gives any unique solutions.

MainWindow()
{
    am = new AudioManager();
    someWidget = new SomeWidget(am);
}

SomeWidget(AudioManager* am_) //SomeWidget does not really care about am
{
    someSubComponent = new SubThingy(am_);
}

SubThingy(AudioManager* am_) : subThingyLocalAudioManagerPtr(am_)
{
    subThingyLocalAudioManagerPtr->registerSomethingOrOther(this);
}
like image 235
learnvst Avatar asked Jun 20 '13 20:06

learnvst


People also ask

How can we avoid using globals?

The simplest way to avoid globals all together is to simply pass your variables using function arguments. As you can see, the $productData array from the controller (via HTTP request) goes through different layer: The controller receives the HTTP request. The parameters are passed to the model.

How do you declare a global variable in Matlab GUI?

If 'global' means, that the variable needs to be shared between callbacks of a figure only, use either set/getappdata, guidata or the 'UserData' of the corrsponding GUI element. This allows e.g. to run two instances of the same GUI without conflicts.

What is the benefit to passing parameters in and out of function instead of using global variables?

The primary advantage of passing a parameter is that, unless it is passed by reference, no values in the main program can be accidentally modified by any use of the parameter variable in the function.

How do you create a global structure?

To declare a structure globally, place it BEFORE int main(void). The structure variables can then be defined locally in main, for example.


1 Answers

In your example, "SomeWidget" should take its actual dependency, "SomeThingy," not an AudioManager.

Usually when you see the whole world referencing a class it means that the class does too much. The name "XyzManager" usually indicates a problem for the same reason. (Classes should be named after what they do, and if the most specific name available that describes what it does is "Manage", then it should be separate classes)

like image 56
Billy ONeal Avatar answered Oct 13 '22 21:10

Billy ONeal