I have a problem with factory pattern when using generics. I have this interface common to everything:
public interface Connection<T> {
}
Obviously I have this implementation:
public class ImplConnection<V> implements Connection<V> {
}
Then I have this factory, which should create an instance of the connection:
public class ConnectionFactory<V, C extends Connection<V>> {
private final Class<V> contentType;
private final Class<C> connectionType;
public ConnectionFactory(Class<V> contentType, Class<C> connectionType) {
this.contentType = contentType;
this.connectionType = connectionType;
}
public C newConnection() {
try {
return connectionType.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
I am trying to instantiate a connection at runtime using this (I use Integeras a parameter for a generic type):
connectionFactory = new ConnectionFactory<Integer, Connection<Integer>>(Integer.class, Connection.class);
but he says:
The constructor ConnectionFactory <Integer,Connection<Integer>>(Class<Integer>, Class<Connection>) is undefined.
Marco source
share