Java泛型类型参数

时间:2017-06-09 15:39:06

标签: java generics

请考虑以下代码:

public abstract class MyObject {
}

public interface Dao<T extends MyObject> {
}

public abstract class JsonDao<T extends MyObject> implements Dao<T> {
}

public abstract class SqliteDao<T extends MyObject> implements Dao<T> {
}

public interface DaoFactory<*????*> {
    public *????* getDao(Class<? extends MyObject> objectClass);
}

现在我想要的是让getDao能够实现JsonDao和SqliteDao,但是使用扩展MyObject的类进行参数化,基本上是这样的:

public interface DaoFactory<T<? extends MyObject> extends Dao<? extends MyObject>> {
    public <U extends MyObject> T<U> getDao(Class<U> objectClass);
}

类似的东西是否可能或者类型擦除是否会阻止它?

2 个答案:

答案 0 :(得分:1)

这是否符合您的需求?

public interface DaoFactory<O extends MyObject, D extends Dao<O>> extends Dao<O> {
    public D getDao(Class<O> objectClass);
    //or for any subclass
    public <RO extends O, RD extends D<RO>> RD getSubDao(Class<RO> objectSubclass);

    //or maybe this
    public <RO extends O> Dao<RO> getSimpleSubDao(Class<RO> objectSubclass);
}

答案 1 :(得分:1)

您可以按如下方式声明DaoFactory

public interface DaoFactory<D extends Dao<? extends MyObject>> {

    <T extends MyObject> D getDao(Class<T> type);
}

然后,JsonDaoFactory可以实现如下:

public class JsonDaoFactory implements DaoFactory<JsonDao<? extends MyObject>> {

    private final Map<Class<? extends MyObject>, JsonDao<? extends MyObject>> daos =
            new HashMap<>();

    public JsonDaoFactory() {
        daos.put(Contact.class, new JsonContactDao());
        daos.put(Customer.class, new JsonCustomerDao());
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T extends MyObject> JsonDao<T> getDao(Class<T> type) {
        return (JsonDao<T>) daos.get(type);
    }
}

我使用Map使MyObject的每个后代与其对应的JsonDao匹配(此处我使用了ContactCustomer,分别为JsonContactDaoJsonCustomerDao。我已经抑制了一个未经检查的强制转换警告,因为我在构造函数中明确地填充了地图,因此我确信不会有ClassCastException

为了完整性,这里是SqliteDaoFactory的代码:

public class SqliteDaoFactory implements DaoFactory<SqliteDao<? extends MyObject>> {

    private final Map<Class<? extends MyObject>, SqliteDao<? extends MyObject>> daos =
            new HashMap<>();

    public SqliteDaoFactory() {
        daos.put(Contact.class, new SqliteContactDao());
        daos.put(Customer.class, new SqliteCustomerDao());
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T extends MyObject> SqliteDao<T> getDao(Class<T> type) {
        return (SqliteDao<T>) daos.get(type);
    }
}

两家工厂的代码几乎都是重复的,但我找不到解决这个问题的方法。

以下是如何使用这些工厂:

JsonDaoFactory jsonDaoFactory = new JsonDaoFactory();
JsonDao<Contact> contactJsonDao = jsonDaoFactory.getDao(Contact.class);
JsonDao<Customer> customerJsonDao = jsonDaoFactory.getDao(Customer.class);

SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
SqliteDao<Contact> contactSqliteDao = sqliteDaoFactory.getDao(Contact.class);
SqliteDao<Customer> customerSqliteDao = sqliteDaoFactory.getDao(Customer.class);