Recently I saw some of the developers coding their VOs with nested builder class like
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public static class UserBuilder {
private String firstName;
private String lastName;
public User build() {
User user = new User();
user.firstName = firstName;
user.lastName = lastName;
return user;
}
public UserBuilder withFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public UserBuilder withLastName(String lastName) {
this.firstName = firstName;
return this;
}
}
}
Now, they claim that this makes code more readable. My point is, this has following disadvantages:
-
I can't simply add fields and expect my IDE to complete code for me, as now I need to update this inner class too.
-
Simple POJOs are carrying code which is not relevant for VO.
I am looking for any advice if I am missing something here. Feel free to add your thoughts about the same.
Sample code after this modification looks like,
User user = User.new UserBuilder()
.build()
.withFirstName("Name")
.withLastName("surName");
Aucun commentaire:
Enregistrer un commentaire