英文:
Builder Pattern Java: How to reference generic type in BaseBuilder for Generic BaseClass
问题
以下是翻译好的内容:
子类:
public class Sub extends Base<T> {
private final String key;
private Sub(Builder builder) {
super(builder);
this.key = builder.key;
}
public static class SubBuilder extends Base.BaseBuilder<SubBuilder> {
String key;
public SubBuilder key(String key) {
this.key = key;
return this;
}
@Override
public Sub build() {
return new Sub(this);
}
}
}
基类:
public class Base<T> {
private final T type;
protected Base(BaseBuilder<?> builder) {
this.type = builder.type;
}
public static class BaseBuilder<B extends BaseBuilder<B>> {
T type; // 这显然不正确,因为 T 不是静态引用
public B type(T type) {
this.type = type;
return (B) this;
}
public Base build() {
return new Base(this);
}
}
}
正如提到的,无法在 BaseBuilder
中引用 T 类型。如何在这里使用构建器设置 T 呢?也不能从 BaseBuilder
中去掉 static。对于这种问题,建造者模式是否合适?
英文:
I want to implement Builder pattern for Generic Base class and Sub class stuck at defining Generic type in Base Builder. Here are the classes.
Sub Class:
public class Sub extends Base<T> {
private final String key;
private Sub(Builder builder) {
super(builder);
this.key = builder.key;
}
public static SubBuilder extends Base.BaseBuilder<SubBuilder> {
String key;
public SubBuilder key(String key) {
this.key = key;
return this;
}
@Override
public Sub build() {
return new Sub(this);
}
}
}
Base Class :
public class Base<T> {
private final T type;
protected Base(BaseBuilder<?> build) {
this.type = build.type;
}
//Base Builder
public static class BaseBuilder<B extends BaseBuilder<B>> {
T type; //This is obviously not right because T is not static reference
public B type(T type) {
this.type = type;
return (B)this;
}
public Base build() {
return new Base(this);
}
}
}
As mentioned can't reference T type in BaseBuilder. How to set T using builder here.
Can't remove static from BaseBuilder too.
Is builder pattern suitable for this kind of problems?
答案1
得分: 0
添加 T
类型参数到 BaseBuilder
,Sub
和 SubBuilder
:
public class Base<T> {
private final T type;
protected Base(BaseBuilder<?, T> build) {
this.type = build.type;
}
public static class BaseBuilder<B extends BaseBuilder<B, T>, T> {
private T type;
public B type(T type) {
this.type = type;
return (B) this;
}
public Base<T> build() {
return new Base<>(this);
}
}
}
public class Sub<T> extends Base<T> {
private final String key;
private Sub(SubBuilder<T> builder) {
super(builder);
this.key = builder.key;
}
public static class SubBuilder<T> extends BaseBuilder<SubBuilder<T>, T> {
private String key;
public SubBuilder<T> key(String key) {
this.key = key;
return this;
}
@Override
public Sub<T> build() {
return new Sub<>(this);
}
}
}
英文:
Add T
type parameter to BaseBuilder
, Sub
and SubBuilder
:
public class Base<T> {
private final T type;
protected Base(BaseBuilder<?, T> build) {
this.type = build.type;
}
public static class BaseBuilder<B extends BaseBuilder<B, T>, T> {
private T type;
public B type(T type) {
this.type = type;
return (B) this;
}
public Base<T> build() {
return new Base<>(this);
}
}
}
public class Sub<T> extends Base<T> {
private final String key;
private Sub(SubBuilder<T> builder) {
super(builder);
this.key = builder.key;
}
public static class SubBuilder<T> extends BaseBuilder<SubBuilder<T>, T> {
private String key;
public SubBuilder<T> key(String key) {
this.key = key;
return this;
}
@Override
public Sub<T> build() {
return new Sub<>(this);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论