开发者

The Registry pattern: to use or not to use

开发者 https://www.devze.com 2023-02-19 08:11 出处:网络
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.

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.

The Registry pattern: to use or not to use

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* m开发者_运维技巧ainWindow;
};

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


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.


In terms of testability, it may not be a good idea to use the Registry Pattern. To repost from this article:

Unit testing is predicated on the assumption that you are testing small, discrete units of code that do not have dependencies. This requires that developers do everything they can to either remove dependencies or mock them in such a way that the dependencies are neutralized as contributors to the failure of the unit being tested. In PHP 5, objects are not copied when assigned; instead, their address in a hash table is copied. This means that if you retrieve an object from the registry, and then modify it, every subsequent retrieval from the registry will reflect that modification.

The reason this creates a significant issue is that it prevents you from testing a discrete unit of code. Now, instead of one thing being the variable in a test failure, there are two: the unit of code being tested and the object it retrieved from the registry. Any time there is more than one possibility for failure, the effectiveness of the unit testing is diminished.


Your Registry class and Singleton are OO-ways of global variables. Prefer to make the Params Pane in the MainFrame and to pass it as reference to the tab-panes.

0

精彩评论

暂无评论...
验证码 换一张
取 消