Let's assume two interfaces:
public interface FruitHandler<T extends Fruit>
{
setFruit(T fruit);
T getFruit();
}
public interface Fruit
{
}
Now I want a factory to create FruitHandlers (e.g. AppleHander, OrangeHandler, ...), but the FruitHandlerFactory does not know neccessary about the implementing classes of both interfaces (like in java parameterized generic static factory). The FruitHandlerFactory should work in this way (where OrangeHandler implements FruitHandler and Orange implements Fruit):
FruitHandlerFactory fhf = new FruitHandlerFactory<OrangeHandler,Orange>();
OrangeHandler fh = fhf.create();
Orange orange = (Orange)fh.getFruit();
This should be the factory:
public class FruitHandlerFactory<A extends FruitHandler, B extends Fruit>
{
public FruitHandler create()
{
FruitHandler<Fruit> fh = new A<B>(); //<--- ERROR
fh.setFruit(new B());
return fh;
}
}
Where I get this error:
The type A is not generic; it cannot be parameterized with arguments <B>
BTW: Is it possible to make the create() method static?