Sto cercando un modello più astratto per i costruttori che gestiscono le richieste campi senza la necessità di scrivere un validatore che controlli se tutti i campi richiesti sono impostati.
Mi piace questo costruttore. Ma c'è un modo per rendere le interfacce più astratte in modo che non sia necessario creare una nuova interfaccia per ogni campo richiesto?
public class Example {
private String first;
private String second;
private String third;
private String fourth;
private String fifth;
public static RequiredSecond builder(String first) {
return new ExampleBuilder(first);
}
public interface RequiredSecond {
RequiredThird withSecond(String second);
}
public interface RequiredThird {
RequiredFourth withThird(String third);
}
public interface RequiredFourth {
Build withFourth(String fourth);
}
public interface Build {
Build withFifth(String fifth);
Example build();
}
private static class ExampleBuilder implements RequiredSecond, RequiredThird, RequiredFourth, Build {
Example example = new Example();
public ExampleBuilder(String first) {
example.setFirst(first);
}
@Override
public Build withFifth(String fifth) {
example.setFifth(fifth);
return this;
}
@Override
public Example build() {
return example;
}
@Override
public Build withFourth(String fourth) {
example.setFourth(fourth);
return this;
}
@Override
public RequiredFourth withThird(String third) {
example.setThird(third);
return this;
}
@Override
public RequiredThird withSecond(String second) {
example.setSecond(second);
return this;
}
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getSecond() {
return second;
}
public void setSecond(String second) {
this.second = second;
}
public String getThird() {
return third;
}
public void setThird(String third) {
this.third = third;
}
public String getFourth() {
return fourth;
}
public void setFourth(String fourth) {
this.fourth = fourth;
}
public String getFifth() {
return fifth;
}
public void setFifth(String fifth) {
this.fifth = fifth;
}
@Override
public String toString() {
return "Example [first=" + first + ", second=" + second + ", third=" + third + ", fourth=" + fourth +
", fifth=" + fifth + "]";
}
}
public class Labb {
public static void main(String[] args) {
// All required including optional
Example ex1 = Example.builder("first").withSecond("second").withThird("third").withFourth("fourth").withFifth("optional fith").build();
System.out.println(ex1);
// Output: Example [first=first, second=second, third=third, fourth=fourth, fifth=optional fith]
// All required excluding optional
Example ex2 = Example.builder("first").withSecond("second").withThird("third").withFourth("fourth").build();
System.out.println(ex2);
// Output: Example [first=first, second=second, third=third, fourth=fourth, fifth=null]
}
}