1

Suppose I'am writting a GUI desktop application which has a main window with a corresponding presenter. This main windows may host one or multiple panels which each also have their own presenter. The main window has a an input field where the user can set a global option. The panel sub views need to react to a change of the global option. When the user changes the global option the main presenter is called, stores the value and notifies its view (the main window) of the data change.

Now, my question is: How do I inform these panel sub views respectively their controllers of the changed global plot duration in a clean way respecting the pattern? I see multiple options but none really feels like a clean solution:

  1. The view notifies all the panel sub view of the changed gobal option after it was called by the presenter.
    The problem with this is, that the view is now handling the logic of informing the panels, even though the main window controller already knows that there are sub panels (To be specific: The main window controller does not have any object references to the panel view objects or their controller objects, only an id). This reduces testability in my opinion and, while working, doesn't feel clean.

  2. There could be a shared model between the main presenter and the presenters of the panel sub views.
    If a new panel is created a reference to the main model would have to be passed into the panel presenter. The model would also need to have an observer approach to inform all the presenters of the data change. Until now I thought it's not typical that the model emits events when the data changes in MVP

  3. The presenter informs the panel presenters of the change.
    For this the presenter would need to have a reference to each panel presenter which is currently not needed. This would complicate the data flow a bit and the moment of passing the panel presenter to the main presenter would be unclear to me, because the panel views create their presenters and the panel views themselves are created as a reaction of the presenter notifying the view to create a new panel.

May be I am missing an approach or you have any Idea how to structure this in a better way.

1 Answer 1

2

Option #2 is fine. It works even if the presenters/views are not hierarchically organized, and the "global option" may affect different components than just panels in your application. In fact, the different views or presenters don't have to know each others and stay completely independent from each other.

We implemented a similar approach in a standard desktop application several years ago, using a global "event handler" class, which provided a publisher/subscriber mechanism for different forms/views which needed to get informed on changes to some global application state (in fact, the number of global state variables managed by that mechanism is less than five). The feature is still in use and works like a charm.

Note the instance where your panels have to subscribe for events does not have to be "the model" itself, directly, it could be a middle-man between the real model objects and the UI or presenter objects. The middle-man object (which should have the lifetime of the application) makes the whole system independent from the lifetime of the collaborating objects / views / presenters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.