jeudi 6 septembre 2018

Generic class with two class hierarchies

I have the following Java generics question

I have the following generic class thay may be sketched as:

public class MyClass<T> {
    AnotherClass<T> another;
    OtherClass<T> other;
    ...
}

where ... represents code that is not relevant to the case.

For the class MyClass<T> is not as important which exact type T is (as of now) but for both:

  • AnotherClass<T>

  • OtherClass<T>

is absolutely crucial what the generic type is and decisions will be made at runtime in base of that.

Based on that, the type T is not completely arbitrary, it may be either an instance of a hierarchy of classes T_1 or a hierarchy of classes T_2.

As is defined the class, the type T is equivalent to Object but I know that is equivalent to either T_1 or T_2

There is not businnes relation between entities T_1 and T_2 therefore I'm not doing:

public interface BaseT { ... }
public class T_1 implements BaseT { ... }
public class T_2 implements BaseT { ... }

public class MyClass<T extends BaseT>

My question is, which alternatives do I have here other than design an interface for MyClass and implement it for both T_1 and T_2?

Kind regards

Aucun commentaire:

Enregistrer un commentaire