dimanche 21 janvier 2018

Builder with conditional inclusion of element

I've been wondering is possible to do Builder with optional parameters more elegantly:

what I have: Object with name, id, age.

I have complex condition for inclusion of age and I'd like to send it to builder on success of that condition but i'd like to have it elegant one liner with one parameter.

what i have so far:

 Builder.name("name").id("id").age(age, complexCondition).build();

or

 Builder builder = Builder.name("name").id("id");
 if(complexCondition){
     builder.age(age);
 }

are there any better options? I'd like to resolve condition where I have it without over-engineering builder and without overcoding for every complex condition check.

Aucun commentaire:

Enregistrer un commentaire