Generics and API Design

I am creating a library for the product that we will release, and I am trying to create it to be compatible with some of the design changes that are currently on the roadmap. In particular, the library needs to process different versions of the product that have the same commands, but which have different requirements for the available parameter parameters. To do this, I have an abstract class for the product with specific classes for each version. In addition, I have an abstract class for peripherals, which changes and specific classes for specific peripherals. I want specific versions to implement an abstract method from an abstract class, but for the specified type T instead of specifying a superclass and then checking instanceof. For example:

class PeripheralA {}

class PeripheralB {}

abstract class AbstractProduct<T> {

     public abstract void SomeFunction(T param);
}

class ProductA extends AbstractProduct<PeripheralA> {
     public void SomeFunction(T param);
}

class ProductB extends AbstractProduct<PeripheralB> {
     public void SomeFunction(T param);
}

, , SomeFunction. , . , PeripheralB ProductA, cast, , , ( ). , , ?

+4
1

T ; , , PeripheralA, - :

class ProductA extends AbstractProduct<PeripheralA> {
    public void SomeFunction(PeripheralA param) {
     // your impl here
    }
+3

All Articles