I'm trying to implement a Strategy + Factory pattern using generics. The goal is to return to a client class an implementation of the interface DocumentDao that can deal with a type T extends Document, so I've multiple Dao interface extending DocumentDao for different subtypes of Document.
Here is my code:
public class Document { ... }
public class DocumentA extends Document { ... }
public class DocumentB extends Document { ... }
public interface DocumentDao<T extends Document> {
public void update(T document);
}
public interface DocumentADao<DocumentA> {}
public interface DocumentDaoFactory {
public DocumentDao<T extends Document> getDaoInstance(Class<T> clazz);
}
Then I try to use the Factory:
private <T extends Document> void someMethod(T document) {
...
DocumentDao<T> documentDao = this.documentDaoFactory.getDaoInstance(document.getClass());
documentDao.update(document);
...
}
But the compiler complaints about the getDaoInstance() call:
Type mismatch: cannot convert from DocumentDao<? extends AbstractGDriveDocument<?>> to DocumentDao<T>
How to deal with this situation? How can I obtain a similar solution?
Thanks
Aucun commentaire:
Enregistrer un commentaire