5

While designing application with MVP architecture I came out with question about instantiating view. In my case presenter must be fully separated from view for possibility of easy changing of UI toolkits. As long as there is only one view everything is clear. My problem comes when there is a need of creating views dynamicly. For example when new button is clicked dialog popup asking user to fill some data. I come with two solutions but I'm curious about others. I'm giving sample code below of my approaches.

  1. Let's parent view to instantiate dialog view and return it to parent presenter where is dialog presenter also instantiate. I don't like the idea that concrete view must instantiate another view. I have a feeling that instantiating is not responsibility of view.

    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's NewDialogPresenter deal with user input */
      }
    
      public View getView() {
        return view;
      }
    }
    
  2. Inject factory to presenter. This factory is responsible for instanting view. There's some problem with passing parent view to dialog view (usually GUI frameworks needs it). Also there is a quite large amount of setup.

    // 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's NewDialogPresenter deal with user input
      }
    
      public View getView() {
        return view;
      }
    
      public void setNewDialogViewFactory(NewDialogViewFactory newDialogViewFactory) {
        this.newDialogViewFactory = newDialogViewFactory;
      }
    }
    

What is your opinion about responsibility to instantiate new presenter and view? I'm looking for practical example of other people approach. Thank you for your suggestions.

michal.kreuzman
  • 12,170
  • 10
  • 58
  • 70
  • check out [this post](http://stackoverflow.com/questions/3309029/mvp-should-the-view-implement-a-presenters-interface-or-vice-versa) and see if it helps. – Eliran Malka Apr 08 '12 at 07:32

1 Answers1

0

In my case I actually never create views (including dialogs). I make the assumption that there can never be more than one dialog present at any time so I use GIN to instantiate and inject dialogs into the presenter. The presenter then just needs to change the attributes on the dialog (including handlers) and show it. This allows me to manage the creation of all views (and presenters) in a single place using GIN.

  • Thank you for you answer. Problem is that different UI libraries have different memory management. So sometimes dialog must be disposed after close, sometimes acts like singleton and is just re-showed again and again. I want to separate this from presentation logic. – michal.kreuzman Apr 20 '12 at 14:07