Reputation: 195
So i am writing my first Java application using MVP pattern. One of MVPs triads is one-way communications, basically Views are read only.
In case where there is only 1 model - 1 presenter - 1 view, would there be any backtrack of creating new presenter object and not holding reference to it anywhere else than model's Observers list?
To make myself clear, this is how i would 'normally' do that:
private Result result;
private ResultPresenter resultPresenter;
public SomePresenter(SomeInterface view) {
result = new Result();
resultPresenter = new ResultPresenter(result, view.getResultView());
}
and this is how i would like to do that:
private Result result;
public SomePresenter(SomeInterface view) {
result = new Result();
new ResultPresenter(result, view.getResultView());
}
Where ResultPresenter is Observer of Result.
Primary question is, would ResultPresenter get cleared by garbage collector?
Upvotes: 0
Views: 219
Reputation: 6921
Among other things (like initializing the view), ResultPresenter
would instantiate or implement one or more Observers and register them on both the "result" and the "resultView".
Those classes would then hold references to the Observers and notify them as required.
There are several ways to do that, with a common pattern being anonymous inner classes.
Since those anonymous inner classes still hold references to their instantiator, the ResultPresenter
will not be garbage collected.
Another option is to instantiate top level classes as Observers.
In this case, it depends on the Observers' implementation whether the presenter is still referenced.
Finally, the presenter itself can implement the Observers' interfaces (one shudders at the thought). Obviously, model and view would then hold references to the presenter itself, though they think of it as an Observer, and it wouldn't be garbage collected.
Even if it would, that would only mean that you only need it to set up the presentation and link the objects to each other, and no longer require it afterwards, so no harm would be done.
Upvotes: 1