I am trying to develop a Project Management System in my application following the Model View Presenter (MVP) pattern. My question is this, I have seen numerous examples of MVP but I haven't seen one with one Presenter and multiple Views. For example when a user opens a project the same project data can be viewed by a treeview, a datagrid and a chart. How do I cope with that?
For making the code decoupled and easy to maintain I would suggest you to have one presenter for each View even if they look like very similar: so that each view will have its own presentation logic. If you got the same data that needs to be showed on more than one view you could share the View-model between the presenters but again I would suggest you to use different view model for each presenter(even if they are very similar)
The way you would do it, is to abstract your view by placing it behind an interface and then wire up the presenter with the concrete implementation of your view.
That said, I'm not sure you would ever want to do it in the real world. The differences between dealing with a tree view compared to a chart would mean that you end up over generalizing things in your view interface and writing a lot of messy code in your view to fulfil the contract.
I would suggest keeping your presenter to view ratio at 1:1. If you want multiple views over the same data then it is your model that you should share across the presenters, so you are displaying the same data in different ways.
You should almost always have one presenter instance per view instance.
Say you open a CustomerView, and its CustomerViewPresenter. That's one instance of each.
The you open another CustomerView, and another CustomerViewPresenter instance. That's two instances of each.
That doesn't mean that a given presenter always has to use the same view, in fact it should not. The presenter should talk to one view interface. You should be able to swap a real view for a mock view for testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With