Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Registry pattern: to use or not to use

I am considering using the Registry pattern in my application to store weak pointers to some of app's windows and panes. The general structure of the application is shown below.

Application diagram

The application has one MainFrame top level window with few child panes within it. There can be many of TabPane type based tabs. I need to reference the ParamsPane panel from all of my TabPane tabs, so I need a pointer to the ParamsPane object to be stored somewhere. There can be a plenty of options, but the most obvious ones are (1) to store the pointer within the Application singleton object or (2) to create a simple registry class. Something like:

class Registry {
public:
    static MainApp* application;
    static MainWindow* mainWindow;
};

Is this a good practice? What are the benefits and caveats of such an approach?

like image 670
ezpresso Avatar asked Nov 05 '22 00:11

ezpresso


1 Answers

It depends on why you want to reference ParamsPane. I can think of two reasons and two different solutions.

You want to update data in ParamsPane because data in TabPane changed.

If this data is completely separatable from the view, what you should probably do is separate it. This means following the Model-View-Controller pattern. Both ParamsPane and TabPane instances can access the model separatly. So there is no direct reference between the two.

There is some strong link between the two, irrelevant of data.

If the previous mentioned point isn't relevant, and there is a really strong link between the two panels you could consider writing a specific TabPane class which stores a reference to a ParamsPane class.

I feel both these solutions are better than a Singleton or 'Registry' approach. Beware that I haven't heard of this pattern before, but I believe I understand its intent. More info on why global state objects (more specifically singletons) are a bad practice can be found here.

like image 53
Steven Jeuris Avatar answered Nov 12 '22 18:11

Steven Jeuris