建造者模式 Java:如何在BaseBuilder中引用泛型类型以用于通用BaseClass。

huangapple go评论85阅读模式
英文:

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&lt;T&gt; {
 private final String key;
      private Sub(Builder builder) {
       super(builder);
       this.key = builder.key;
     }
public static SubBuilder extends Base.BaseBuilder&lt;SubBuilder&gt; {
     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&lt;T&gt; {
 private final T type;
 protected Base(BaseBuilder&lt;?&gt; build) {
  this.type = build.type; 
}
//Base Builder
public static class BaseBuilder&lt;B extends BaseBuilder&lt;B&gt;&gt; {
 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 类型参数到 BaseBuilderSubSubBuilder

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&lt;T&gt; {
    private final T type;
    
    protected Base(BaseBuilder&lt;?, T&gt; build) {
        this.type = build.type;
    }
    
    public static class BaseBuilder&lt;B extends BaseBuilder&lt;B, T&gt;, T&gt; {
        private T type;
        
        public B type(T type) {
            this.type = type;
            return (B) this;
        }
        
        public Base&lt;T&gt; build() {
            return new Base&lt;&gt;(this);
        }
    }
}
public class Sub&lt;T&gt; extends Base&lt;T&gt; {
    private final String key;

    private Sub(SubBuilder&lt;T&gt; builder) {
        super(builder);
        this.key = builder.key;
    }

    public static class SubBuilder&lt;T&gt; extends BaseBuilder&lt;SubBuilder&lt;T&gt;, T&gt; {
        private String key;

        public SubBuilder&lt;T&gt; key(String key) {
            this.key = key;
            return this;
        }

        @Override
        public Sub&lt;T&gt; build() {
            return new Sub&lt;&gt;(this);
        }
    }
}

huangapple
  • 本文由 发表于 2020年4月4日 11:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61023406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定