So here is the existing code snippet written to build Human
s ( as in matrix
movie :) )
if (gender.equals("male")){
return Human.builder()
.gender('male')
.name('abc')
.speaks("english")
.alive(true)
.build();
}else{
return Human.builder()
.gender('female')
.name('abcd')
.speaks("english")
.alive(true)
.build();
}
If you look, this code has lot of redundancy in attribute assignment which can be minimised. Now imagine 10 conditions like it (here, its just 2!), no matter what you try, it would eventually lead to ugly looking redundant code.
I tried searching lot of resources online and couldn't find any way to build object as per builder design. What i would like to achive here ( to reduce code redundancy) is something like below :
Human human = Human.builder()
.speaks("english")
.alive(true);
if (gender.equals("male")){
human = human // or just human.gender('male').name('abc'); no assignment
.gender('male')
.name('abc');
}else{
human = human // or just human.gender('female').name('abcd'); no assignment
.gender('female')
.name('abcd');
}
return human.build();
Is this possible via lombok or anyone knows a better way to build objects here?
If its worth it, i am on drop-wizard
if (gender == "male")
? Really? – Ciracircaif (gender.equals("male"))
! – Ciracirca=
. Thanks anyway for pointing out, ill update! – Soria