Java 泛型工厂模式



我在使用泛型时遇到工厂模式问题。我有这个接口,在所有内容上都是通用的:

public interface Connection<T> {
    /* methods */
}

显然,我有这个实现:

public class ImplConnection<V> implements Connection<V> {
    /* body */
}

然后我有这个工厂,它必须创建一个连接实例:

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);
        }
    }
}

正在尝试使用以下命令在运行时实例化连接(我使用 Integer 作为泛型类型的参数):

connectionFactory = new ConnectionFactory<Integer, Connection<Integer>>(Integer.class, Connection.class);

但它说:

The constructor ConnectionFactory <Integer,Connection<Integer>>(Class<Integer>, Class<Connection>) is undefined.

传递类参数时,Connection 不会扩展Connection<Integer> 。所以Class<Connection>不能作为参数给出Class<? extends Connection<Integer>>。这就是隐藏在错误背后的内容。

如果你想保持这种模式,你应该做的是像这样:

public class IntegerConnection implements Connection<Integer> {}

这将起作用。

但是,一般来说,您知道可以创建通用实例而无需键入任何特殊内容吗?

public class ConnectionFactory {
  public <T> Connection<T> newConnection() {
    return new ConnectionImpl<T>();
  }
}

你可以像这样使用它:

Connection<Integer> connection = connectionFactory.newInstance();

当我使用泛型时,我经常使用番石榴TypeToken。他们非常有帮助。你的类可能是这样的:

public class ConnectionFactory<V, C extends Connection<V>> {
    private final TypeToken<V> contentType;
    private final TypeToken<C> connectionType;
    public ConnectionFactory() {
        this.contentType = new TypeToken<V>(getClass()) {};
        this.connectionType = new TypeToken<C>(getClass()) {};
    }
    public C newConnection() {
        try {
            return (C) connectionType.getRawType().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

一试。

最新更新