[英]How get Instance using Generic approach
I am trying to build one page assembler using generic approach.我正在尝试使用通用方法构建一页汇编程序。 Following is piece of code.
以下是一段代码。 In this code IEntity is marker interface for DB entities.
在此代码中 IEntity 是 DB 实体的标记接口。
public abstract class PageHrefBuilder implements HrefBuilder<IEntity, PageLinks> {
@Override
public PageLinks buildLinks(IEntity entity) {
return null;
}
}
public interface HrefBuilder<E extends IEntity, L extends Links> {
public L buildLinks(E dto);
}
So we have one interface says can build links using IEntity type of class and return Links type of value.所以我们有一个接口说可以使用 IEntity 类型的类构建链接并返回 Links 类型的值。 So I want to write some common code in abstract class and abstract class does not know what type entity it suppose to deal with.
所以我想在抽象类中编写一些通用代码,而抽象类不知道它想处理什么类型的实体。 For example entity can UserEntity, OrderEntity and so on.
例如实体可以是 UserEntity、OrderEntity 等。
So my question is how in abstract class I can get the class instance to build links with using instanceof or if else approach.所以我的问题是如何在抽象类中使用 instanceof 或 if else 方法获取类实例来构建链接。
Could someone help me on this.有人可以帮我解决这个问题。
You can make your buildLinks method to take Class<T>
parameter instead of the object you have to pass.您可以让buildLinks方法采用
Class<T>
参数而不是您必须传递的对象。
so it will be所以它会
public L buildLinks(Class<E> dto);
then in your abstract class然后在你的抽象类中
@Override
public PageLinks buildLinks(Class<IEntity> dto) {
return dto.newInstance();
}
Hope this answer could help you.希望这个回答能帮到你。
This got a bit over-engineered but it works and all generics are proper.这有点过度设计,但它有效并且所有泛型都是正确的。 Hopefully it shows you how you can build your solution:
希望它向您展示如何构建您的解决方案:
public class GenericsSample {
public abstract class Links {
String data;
public Links(String data) {
this.data = data;
}
}
public class UserLinks extends Links {
public UserLinks(String data) {
super(data);
}
}
public class PageLinks extends Links {
public PageLinks(String data) {
super(data);
}
}
public abstract class IEntity<L extends Links> {
public abstract L buildLinks();
}
public class UserEntity extends IEntity<UserLinks> {
@Override
public UserLinks buildLinks() {
return new UserLinks("From UserEntity");
}
}
public class PageEntity extends IEntity<PageLinks> {
@Override
public PageLinks buildLinks() {
return new PageLinks("From PageEntity");
}
}
public interface HrefBuilderInterface<E extends IEntity<L>, L extends Links> {
public L buildLinks(E dto);
}
public class HrefBuilder<E extends IEntity<L>, L extends Links> implements HrefBuilderInterface<E, L> {
@Override
public L buildLinks(E entity) {
return entity.buildLinks();
}
}
public static void main(String[] args) {
new GenericsSample().go();
}
private void go() {
System.out.println(new HrefBuilder<UserEntity, UserLinks>().buildLinks(new UserEntity()).data);
System.out.println(new HrefBuilder<PageEntity, PageLinks>().buildLinks(new PageEntity()).data);
}
}
Also note that thanks to this setup the following is not valid:另请注意,由于此设置,以下内容无效:
System.out.println(new HrefBuilder<PageEntity, PageLinks>().buildLinks(new UserEntity()).data);
You can declare abstract method
in abstract class to get new instance of Links
interface:您可以在抽象类中声明
abstract method
来获取Links
接口的新实例:
abstract class PageHrefBuilder<E extends IEntity, L extends Links> implements HrefBuilder<E, L> {
@Override
public L buildLinks(E dto) {
L links = newLinks();
// ...
return links;
}
protected abstract L newLinks();
}
and implement getting
new instance in realization.并实现在实现中
getting
新实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.