I have a very unique requirement with Builder pattern. I have the following code using Builder pattern:
Detail {
private String field1;
private String field2;
private String field3;
Detail(String field1, String field2) {
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
}
public class DetailBuilder {
private String field1;
private String field2;
private String field3;
public DetailBuilder field1(String field1) {
this.field1 = field1;
return this;
}
public DetailBuilder field2(String field2) {
this.field2 = field2;
return this;
}
public DetailBuilder field3(String field3) {
this.field3 = field3;
return this;
}
public Detail build() {
return new Detail(field1, field2, field3);
}
}
}
Now the problem is I have a class with publicly exposed methods for clients to use and they all take Detail object as parameter.
class ClientClass {
public void method1(Detail detail) {
// this method needs field1 & field2 compulsorily and doesn't require field3
}
public void method2(Detail detail) {
// this method needs field2 & field3 compulsorily and doesn't require field1
}
public void method3(Detail detail) {
// this method needs field1 compulsorily and doesn't require field2 and field3
} and so on
}
Now how do I let the clients know during compile time about what values are required for each method using the builder object?
Any help would be greatly appreciated!!
Aucun commentaire:
Enregistrer un commentaire