Possibly Java recursive generics between two classes

I am trying to create several classes for managing a sample presenter application. I came up with the following definitions, but I try to avoid recursive generics.

public abstract class Presenter<V extends View<...?>> {

  protected V view;

  public Presenter(V view) {
    this.view = view;
  }

  // ...
}


public abstract class View<P extends Presenter<...?>> {

  protected P presenter;

  // ...
}

I wanted to establish a relationship between the two classes. The idea was that I could create a presenter for a specific presentation, both classes relying on useful methods defined in abstract base classes, but both know exactly which subclass the abstract abstract class is used for.

My problem is defining a piece of code ..?. I see no way to avoid a recursive situation, for example:

public abstract class View<P extends Presenter<V>, V extends View<Q>, Q extends...>

and even this definition is consistent, since the View class now accepts two common parameters ... massive confusion.

, , , :

// simpler option

public abstract class Presenter {

  protected View view;    

  public Presenter(View view) {
    this.view = view;
  }
}

public class FooPresenter extends Presenter {

  public FooPresenter(BarView view) {
    super(view);
  }

  public someMethod() {
    ((BarView) getView()).viewSpecificMethod();
  }
}

, "" .

+5
2

public abstract class Presenter<V extends View<? extends Presenter<?>>>

public abstract class View<P extends Presenter<? extends View<?>>>

, , - .

+1

this:

class Presenter<P extends Presenter<P,V>, V extends View<P,V>> {
    V view;
}

class View<P extends Presenter<P,V>, V extends View<P,V>> {
    P presenter;
}

class MyPresenter extends Presenter<MyPresenter, MyView>{}

class MyView extends View<MyPresenter, MyView>{}

:

MyPresenter mp = new MyPresenter().view.presenter;
+3

All Articles