Search code examples
javamvp

MVP: retrieving view components efficiently


I have an app which Views contain several widgets, and these widgets are made of other widgets

For which, when I want to use something from the presenter I have to do this:

getView().getWdigetA().getWidgetB().getWidgetC().setValue("example");

  • Are there any practices on how to write Views?.
  • Also should the methods return widgets or just modify values? and last,
  • Does it help to be neater?

Solution

  • It sounds like you are doing the exact opposite of MVP. With MVP you have a model which represents your data, the presenter which is responsible for formatting your data (to locales, i18n, etc..), and your view which are the actual gui widgets.

    So if you wanted to set a value, you wouldn't do:

    getView().getWdigetA().getWidgetB().getWidgetC().setValue("example");
    

    You would instead:

    getModel().setSomeProperty("example");
    

    Which as you can see is much simpler. Applying MVP can take a lot of initial design, thought and architecting. However once you have it setup and working, it really makes a difference in terms of simplicity and maintenance.

    Here is a helpful post about MVP being applied to a JDialog, the answer explains the aspects rather well: Applying the MVP pattern to JDialogs