Question

For context, I'm building a GTK+ application in C where a subclass of GtkApplicationWindow creates and displays a subclass of GtkToolbar and a GtkNotebook (a widget with multiple pages that can be displayed alternately by a member function). There are radio buttons on the toolbar to switch among the pages.

Should the toolbar hold a (duplicate) reference to the notebook (provided by the window during construction), or should it only hold a reference to its owner (the window) and call a function of its owner which in turn calls the notebook's function to switch pages? Another alternative, the toolbar might only keep a reference to window, and access the window's reference of the notebook each time it wishes to toggle pages? I have a feeling this is a case addressed by the Law of Demeter, but I'm not sure what it dictates.

Was it helpful?

Solution

It should only hold a reference to its owner (the window) and call a function of its owner which in turn calls the notebook's function to switch pages. The owner owns the reference to the notebook and can validate whether the pointer is still good vs. has been released.

OTHER TIPS

Have you considered basing this on events? This would allow you to structure the publish/subscribe relationships separately from the object relationships.

Licensed under: CC-BY-SA with attribution
scroll top