I try to figure out the best approach for writing Builder with N generics and inheritance and it seems not so straightforward, probably it's incorrect approach from design perspective but I would be glad to get any suggestion in this situation.
I have a parent class:
public class Base<F extends Element, C extends Rule> {
public Base(Builder<F, C> builder) {}
protected abstract class Builder<F1 extends Element, C1 extends Rule> {
private Class<F1> focusClass = (Class<F1>) Dot.class;
private Class<C1> ruleClass = (Class<C1>) ComplicatedRule.class;
protected Builder() {
}
public Builder<F1, C1> focusClass(Class<F1> focusClass) {
this.focusClass = focusClass;
return this;
}
}
}
And one of the child classes:
public class ChildA<S extends Element, F extends Element, C extends Rule> extends Base<F, C> {
private ChildA(Builder<S, F, C> builder) {
super(builder);
}
public class Builder<S1 extends Element, F1 extends Element, C1 extends Rule>
extends Base<F1, C1>.Builder<F1, C1> {
private Class<S1> searchClass = (Class<S1>) Line.class;
public Builder() {
super();
}
public Builder<S1, F1, C1> searchClass(Class<S1> searchClass) {
this.searchClass = searchTClass;
return this;
}
public ChildA build() {
return new ChildA<>(this);
}
}
}
It fails with compilation error in ChildA
. Does it exist any way to make it less terrifying and simpler?
Aucun commentaire:
Enregistrer un commentaire