在接口扩展中指定泛型 [英] Specify Generics in interface extends
问题描述
我的意思是,如果我有一个接口:
public interface Repo< T> {
Collection< T>搜索(String params);
T get(String id);
}
然后是一大堆特定的存储库,例如 ClientRepo , CustomerRepo 等等...扩展此接口时指定类型 T 是合理的,例如:
public interface ClientRepo extends Repo< Client> {
}
public interface CustomerRepo extends Repo< Customer> {
}
客户和客户只是一些类。
有没有人类似的问题?我的意思是我可以这样做:
public interface ClientRepo< T>扩展Repo< T> {
}
附录:
也许我应该让我的意图是让特定的Repos(例如ClientRepo)更清晰。还有另外一个名为 RepoFactory 的接口,它将适当的回购返回给客户端,例如:
public interface RepoFactory {
ClientRepo createClientRepo();
CustomerRepo createCustomerRepo();
}
该工厂由实施者 ,提供具体Repos的适当实现。
实际上,从上面可以看出, Repo< T> 由api的客户端完成。
足够让我困惑!对不起:($ / b>
原来,我寻找的解决方案只是抛出所有我的专用接口:
$ b $ pre $
public interface RepoFactory {
Repo< Client> createClientRepo();
Repo< Customer> createCustomerRepo();} $
$ b 通过这种方式,我可以保持我的静态类型检查来执行api并获得更多的灵活性用于我的Factory的实现。
Should I specify the concrete type for generic types when extending an interface with another interface?
What I mean is, if I have an interface:
public interface Repo<T>{
Collection<T> search(String params);
T get(String id);
}
and then a whole bunch specific Repositories, like ClientRepo, CustomerRepo, etc... is it reasonable to specify the type T when extending this interface, e.g.:
public interface ClientRepo extends Repo<Client>{
}
public interface CustomerRepo extends Repo<Customer>{
}
where Client and Customer are just some classes.
Did anyone have a similar problem? I mean I could do:
public interface ClientRepo<T> extends Repo<T>{
}
Addendum:
Perhaps I should make my intent for having specific Repos (e.g. ClientRepo) more clear. There is another interface called RepoFactory that returns appropriate Repo to the client, e.g.:
public interface RepoFactory{
ClientRepo createClientRepo();
CustomerRepo createCustomerRepo();
}
This factory is implemented by implementors which in turn, provide the appropriate implementations of the concrete Repos.
In fact from the above you could say that the interface Repo<T> is not used by the client of the api.
Confusing enough I hope!!! Sorry :(
解决方案 Turns out the solution I was looking for was simply to throw out all my specialized interfaces:
public interface RepoFactory{
Repo<Client> createClientRepo();
Repo<Customer> createCustomerRepo(); }
This way I get to both keep my static type-checking to enforce the api and also gain an additional flexibility for implementations of my Factory.
这篇关于在接口扩展中指定泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!