I’ve checked out a few different examples on MVP pattern and I found various implementations of it. Its not clear to me though which one should contain the other: the view or the presenter?
As the name suggests it:
public MyPresenter(){
display = new MyView(this);
}
or as my sane logic(and a wikipedia page about MVP)
public MyView(){
presenter = new MyPresenter(this);
}
or should I use something completly different approach creating these objects?
DOM is one of the most costly thing in browsers, so constructing widgets is costly in GWT. On the contrary, presenters are generally cheap, so they can be trashed and reconstructed at will.
That’s why I’d recommend as a rule of thumb to try to reuse your views (make them singletons, or cache them for a few minutes) across many presenters.
This means your (short-lived) presenter should reference your (long-lived) view.
I’m a strong believer in dependency injection, so I wouldn’t instantiate one from within another. This greatly helps in managing the components’ lifetime, each one independently ofthe other.
So you’d inject your view into your presenter: create the presenter, pick the view (either create it or get it from your cache) and give it to your presenter. Then you’ll destroy the presenter and keep the view around to be reused the next time you need it (with another presenter).