开发者

Generic factory with unknown implementation classes

开发者 https://www.devze.com 2023-03-07 22:15 出处:网络
Let\'s assume two interfaces: public interface FruitHandler<T extends Fruit> { setFruit(T fruit); T getFruit();

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开发者_如何学Go 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?


Since generics in Java are implemented using erasure, the type information of FruitHandlerFactory will not be available at runtime, which means you can't instantiate A (or B) this way.

You can, however pass in a Class object of the correct type to work around this:

public class FruitHandlerFactory<H extends FruitHandler<F>, F extends Fruit> {
    final Class<H> handlerClass;
    final Class<F> fruitClass;

    public FruitHandlerFactory(final Class<H> handlerClass, final Class<F> fruitClass) {
        this.handlerClass = handlerClass;
        this.fruitClass = fruitClass;
    }

    public H create() throws InstantiationException, IllegalAccessException {
        H handler = handlerClass.newInstance();
        handler.setFruit(fruitClass.newInstance());
        return handler;
    }
}

A minor drawback is that you'll have to write the type names three times(1) if you want to instantiate a FruitHandlerFactory:

FruitHandlerFactory fhf = new FruitHandlerFactory<OrangeHandler,Orange>(OrangeHandler.class, Orange.class);

You can somewhat reduce that by producing a static createFactory() method on your FruitHandlerFactory:

static <H extends FruitHandler<F>, F extends Fruit> FruitHandlerFactory<H, F> createFactory(
        final Class<H> handlerClass, final Class<F> fruitClass) {
    return new FruitHandlerFactory<H, F>(handlerClass, fruitClass);
}

and use it like this:

FruitHandlerFactory fhf = FruitHandlerFactory.createFactory(OrangeHandler.class, Orange.class);


From this question:

Perhaps try this?

public class FruitHandlerFactory<A extends FruitHandler<B>, B extends Fruit>
0

精彩评论

暂无评论...
验证码 换一张
取 消