Question

I'm making a contacts app, and I'm in a point where I have two views and two presenters (contactList and contactForm), both of the views have a toolbar, and in that toolbar I need to make a search widget with some complex logic.

For one thing, I have to implement some clickHandlers for the widget, and there must be just one instance of the logic, so I can't just make two copies of handlers in both presenters.

Right now I think I need to make a new searchPresenter with the handlers' logic, but how would I instantiate the new presenter? Should it be instantiated in the other presenters? How about the view, should it be instantiated in the other views?

So that's what I'm inclined to do:

in both contactListView and contactFormView create the searchView

toolbox.add(new SearchView());

in both presenters create the searchPresenter and hand over the view (note that I need to get the view in the ContactListPresenter, which I would wish to avoid, I don't want presenter to know anything about the view):

initSearchPresenter() {
  this.searchPresenter = new SearchPresenter(view.getSearchView)
}

and in the controller add an initSearchPresenter call, next to creating both presenters:

contactForm = new ContactFormPresenter(new ContactFormView());
contactForm.initSearchPresenter();
contactList = new ContactListPresenter(new ContactListView());
contactList.initSearchPresenter();

My question is, is this the correct way of solving this issue, or should I make an entirely different thing?

No correct solution

OTHER TIPS

You have to create a view in each widget you want to display it. But you can use the same presenter for both instance of the view.

You have two ways to do this.

1° The cleanest approach is using Gin: define your ContactFormPresenter as a singleton and inject it in both views.

2° Using a "ClientFactory". Create the ContactFormPresenter in the client factory and give its reference to your two views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top