If you're using a language that supports oriented object in your project, probably there's some lines of codes with Builder pattern. If not, this post will help you to understand about it.
What's Builder Pattern?
Builder Pattern belongs to an area in Software Engineer called Design Patterns, the idea behind of a pattern is to solve commons problems in your project following best practices. Builder Pattern is very useful when we need to provide a better solution in the creational objects part in our project.
Sometimes we need to instantiate an object with a lot of parameters and this could be a problem if you pass a wrong parameter value. Things like this happen every time and results in bugs and you will need to find out where's the issue and maybe, refactoring code to improve it.
Let's write some lines of code to see how does Builder Pattern works and when to apply it.
The code below is an example of a traditional Class with constructor used to load values when the object instantiated.
public class PersonalInfo {
private final String firstName;
private final String lastName;
private final Date birthDate;
private final String address;
private final String city;
private final String zipCode;
private final String state;
private final int population;
public PersonalInfo(String firstName, String lastName,
Date birthDate, String address,
String city, String zipCode,
String state, int population){
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.address = address;
this.city = city;
this.zipCode = zipCode;
this.state = state;
this.population = population;
}
}
And now, we can instantiate the object simulating the client code.
PersonalInfo personalInfo
= new BuilderPattern("Mônica", "Avelar",
new Date(), "23 Market Street",
"San Francisco", "94016",
"CA", 800000);
If you notice, to instantiate the object we should pass all the values related to each property of our class and there's a big chance to pass a wrong value. Another disadvantage of this approach is the possibility to not scale it.
In this example we have a few properties but tomorrow we can add more properties and the disadvantage becomes clearer.
Working with Builder Pattern
Let's rewrite the code above to the Builder Pattern and see the differences.
public class PersonalInfo {
private final String firstName;
private final String lastName;
private final Date birthDate;
private final String address;
private final String city;
private final String zipCode;
private final String state;
private final int population;
public static class Builder {
private String firstName;
private String lastName;
private Date birthDate;
private String address;
private String city;
private String zipCode;
private String state;
private int population;
public Builder firstName(String value) {
firstName = value;
return this;
}
public Builder lastName(String value) {
lastName = value;
return this;
}
public Builder birthDate(Date value) {
birthDate = value;
return this;
}
public Builder address(String value) {
address = value;
return this;
}
public Builder city(String value) {
city = value;
return this;
}
public Builder zipCode(String value) {
zipCode = value;
return this;
}
public Builder state(String value) {
state = value;
return this;
}
public Builder population(int value) {
population = value;
return this;
}
public BuilderPattern build() {
return new BuilderPattern(this);
}
}
public PersonalInfo(Builder builder){
firstName = builder.firstName;
lastName = builder.lastName;
birthDate = builder.birthDate;
address = builder.address;
city = builder.city;
zipCode = builder.zipCode;
state = builder.state;
population = builder.population;
}
}
If you compare both codes you will conclude that the first one is smaller and better to understand than the second one and I agree it. The advantage of the usage is going to be clear for the next example when we create an object based on Builder pattern.
Simulating client code using Builder Pattern
PersonalInfo personalInfo =
new Builder()
.firstName("Mônica")
.lastName("Avelar")
.birthDate(new Date())
.address("23 Market Street")
.city("San Francisco")
.zipCode("94016")
.state("CA")
.population(80000)
.build();
This last example of creation object using Builder Pattern turns an organized code following the best practices and easy to read. Another advantage of Builder is that we can identify each property before passing values.
To be honest I've been using Builder Pattern in my projects and I strongly recommend you do it the same in your next projects. There's an easier way to implement Builder pattern in projects nowadays and I'll write a post about it, see you soon!
Books to study and read
If you want to learn more about and reach a high level of knowledge, I strongly recommend reading the following book(s):
Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software is a book that through Java examples shows you the patterns that matter, when to use them and why, how to apply them to your own designs, and the object-oriented design principles on which they're based.
Design Patterns com Java. Projeto Orientado a Objetos Guiado por Padrões (Portuguese version) is a book that shows the concepts and fundamentals of Design Patterns and how to apply for different contexts using Java language.
Commenti