Responsibility for creating an instance in MVP

When developing an application with MVP architecture, I had a question about creating an instance. In my case, the presenter should be completely separated from the view in order to easily change the sets of user interfaces. As long as there is only one point of view, everything is clear. My problem arises when there is a need to dynamically create views. For example, when you press a new button, a dialog box appears asking you to fill in some data. I have two solutions, but I am interested to know about others. I give sample code below my approaches.

  • Let the parent view instantiate the dialog box and return it to the parent presenter, where the dialog presentation is also created. I do not like the idea that a particular look should create a new point of view. I have the feeling that creating an instance is not the responsibility of viewing.

    public interface View { public void setPresenter(Presenter presenter); public void showView(); } public interface NewDialogView implements View { /* ommited ordinary getters/setters for view */ } public interface MainWindowView implements View { /* ommited ordinary getters/setters for view */ public NewDialogView createNewDialog(); } public interface Presenter { public View getView(); } public class NewDialogPresenter implements Presenter { protected NewDialogView view; public MainWindow(NewDialogView view) { this.view = view; this.view.setPresenter(this); } public View getView() { return view; } } public class MainWindowPresenter implements Presenter { protected MainWindowView view; public MainWindow(MainWindowView view) { this.view = view; this.view.setPresenter(this); } public void newButtonClicked() { NewDialogView newDialogView = view.createNewDialog(); // too much responsibility? NewDialogPresenter newDialogPresenter = new NewDialogPresenter(newDialogView); newDialogView.showView(); /* then let NewDialogPresenter deal with user input */ } public View getView() { return view; } } 
  • Paste the factory into the presentation. This factory is responsible for browsing. There is some kind of problem with passing the parent view to the dialog box (usually this requires a GUI interface). There is also a fairly large set of settings.

     // Just presenter approach differs and there is no create method in view interface. public interface MainWindowView implements View { /* ommited ordinary getters/setters for view */ } public class MainWindowPresenter implements Presenter { protected MainWindowView view; protected NewDialogViewFactory newDialogViewFactory; public MainWindow(MainWindowView view) { this.view = view; this.view.setPresenter(this); } public void newButtonClicked() { NewDialogView newDialogView = newDialogViewFactory.createNewDialog(view); // should pass a parent view here or not? NewDialogPresenter newDialogPresenter = new NewDialogPresenter(newDialogView); newDialogView.showView(); // then let NewDialogPresenter deal with user input } public View getView() { return view; } public void setNewDialogViewFactory(NewDialogViewFactory newDialogViewFactory) { this.newDialogViewFactory = newDialogViewFactory; } } 

What is your opinion on the responsibility for creating a new presenter and presentation? I am looking for a practical example of other people's approach. Thank you for your suggestions.

+6
source share
1 answer

In my case, I actually never create views (including dialogs). I believe that at any time there can be no more than one dialog box, so I use GIN to create and enter dialogs in the presenter. Then the facilitator just needs to change the attributes in the dialog (including handlers) and show it. This allows me to control the creation of all the views (and presenters) in one place using the GIN.

+1
source

Source: https://habr.com/ru/post/912049/


All Articles