Builder Pattern Usage in Real Life
Join the DZone community and get the full member experience.
Join For FreeBuilder pattern is probably one of the easiest (except singleton) to implement and to use it. It’s very convenient to use, when an entity has a lot of constructors and becomes very hard for external user of entity to decide which one to use.
Builder pattern helps to avoid such confusion, because the user can decide for which fields set values and for which not.
Also, I like to use the Builder pattern when I have an entity which consists of a lot of fields and I need to test some service, but before that I need to set some information to an entity. Let’s say we have a Person entity:
public class Person {
private String firstName;
private String lastName;
private LocalDate birthDate;
private String addressOne;
private String addressTwo;
private String sex;
private boolean driverLicence;
private boolean married;
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 LocalDate getBirthDate() {
return birthDate;
}
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
public String getAddressOne() {
return addressOne;
}
public void setAddressOne(String addressOne) {
this.addressOne = addressOne;
}
public String getAddressTwo() {
return addressTwo;
}
public void setAddressTwo(String addressTwo) {
this.addressTwo = addressTwo;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean isDriverLicence() {
return driverLicence;
}
public void setDriverLicence(boolean driverLicence) {
this.driverLicence = driverLicence;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
And I have a service, which needs to be tested, but before that, I need to set some information to an entity. I could create an entity with particular information in my test as follows:
private Person createPersonForTesting() {
Person person = new Person();
person.setFirstName("FirstName");
person.setLastName("LastName");
person.setAddressOne("Address1");
...
return person;
}
But at this point it doesn’t look good, it takes a little bit more space and it takes more time to write this method (and speed to us developers is very important, isn’t it?). Also, there is a risk to miss some field.
To avoid all these mentioned problems, better is to use a Builder design pattern:
private Person createPersonForTesting() {
return Person.builder()
.firstName("FirstName")
.lastName("LastName")
.addressOne("AddressOne")
.addressTwo("AddressTwo")
.birthDate(LocalDate.of(1995, Month.APRIL, 13))
.sex("male")
.driverLicence(true)
.married(true)
.build();
}
It looks much nicer, isn’t it?
And here is my builder:
public class Person {
private String firstName;
private String lastName;
private LocalDate birthDate;
private String addressOne;
private String addressTwo;
private String sex;
private boolean driverLicence;
private boolean married;
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 LocalDate getBirthDate() {
return birthDate;
}
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
public String getAddressOne() {
return addressOne;
}
public void setAddressOne(String addressOne) {
this.addressOne = addressOne;
}
public String getAddressTwo() {
return addressTwo;
}
public void setAddressTwo(String addressTwo) {
this.addressTwo = addressTwo;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean isDriverLicence() {
return driverLicence;
}
public void setDriverLicence(boolean driverLicence) {
this.driverLicence = driverLicence;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
public static PersonBuilder builder() {
return new PersonBuilder();
}
public static class PersonBuilder {
private String firstName;
private String lastName;
private LocalDate birthDate;
private String addressOne;
private String addressTwo;
private String sex;
private boolean driverLicence;
private boolean married;
public PersonBuilder firstName(String firstName) {
this.firstName = firstName;
return this;
}
public PersonBuilder lastName(String lastName) {
this.lastName = lastName;
return this;
}
public PersonBuilder birthDate(LocalDate birthDate) {
this.birthDate = birthDate;
return this;
}
public PersonBuilder addressOne(String addressOne) {
this.addressOne = addressOne;
return this;
}
public PersonBuilder addressTwo(String addressTwo) {
this.addressTwo = addressTwo;
return this;
}
public PersonBuilder sex(String sex) {
this.sex = sex;
return this;
}
public PersonBuilder driverLicence(boolean driverLicence) {
this.driverLicence = driverLicence;
return this;
}
public PersonBuilder married(boolean married) {
this.married = married;
return this;
}
public Person build() {
Person person = new Person();
person.firstName = this.firstName;
person.lastName = this.lastName;
person.addressOne = this.addressOne;
person.addressTwo = this.addressTwo;
person.birthDate = this.birthDate;
person.sex = this.sex;
person.driverLicence = this.driverLicence;
person.married = this.married;
return person;
}
}
}
Please notice, that I’m using Building pattern a little bit differently than “officially it’s agreed” (have a look at wiki Java example http://en.wikipedia.org/wiki/Builder_pattern
Instead of having a private constructor of Person class which would take PersonBuilder object, I’m creating static builder() method, which returns PersonBuilder object. And then using this builder object I’m constructing Person.
I’m doing this way, because of two things:
First of all, if I’m refactoring old code, and let’s say the Person object is created in a lot of places with default constructor without arguments, creating private constructor in Person class with arguments forces me to create another public constructor with no arguments (this is unnecessary code) or go and to rewrite the code using a builder.
Secondly, having long constructor in Person class makes it quite ugly in my opinion. Of course, it’s possible to move constructor to the end of the class, but I prefer to have it in nested static class as it gives some kind of separation.
Example source code available here.
Published at DZone with permission of Remigijus Kutkaitis. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments