Using Builder Pattern in Java
Design Patterns can make our work a lot easier. They are a generic solution for recurring design problem. For example some of classes have many attributes with different properties. It requires many constructors and they can be really difficult to use. To solve this problem we can implement the Builder Design Pattern.
Get Pablo Osinaga’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s asume that we are working on an Auto-Management software and we want to write an Auto class with the following attributes:
public class Auto {
private final String name; //required
private double price;
private int year;
private String color;
private double maxSpeed; private List<Owner> owners;
}
The traditional solution would look like this:
public Auto(String name, double price, int year, String color){
this(name,price,year,color,0.0);
}public Auto(String name, double price, int year){
this(name,price,year,”Red”);
}public Auto(String name, double price){
this(name,price,1925);
}public Auto(String name){
this(name,0.0);
}
But as you can see it isn’t flexible at all. We can’t choose for example a constructor with the attributes name and price. We would need an additional constructor for that. That’s why I would recommend you the this Design Pattern.
Step 1: Create a static Builder class
The first step consist in writing a static class with in our class. In our example the AutoBuilder class. This builder class should have the same attributes as the regular class.
public class Auto { private final String name; //required
private double price;
private int year;
private String color;
private double maxSpeed;
private List<Owner> owners; static class AutoBuilder{ private final String name; //required
private double price;
private int year;
private String color;
private double maxSpeed;
private List<Owner> owners;
}
}
Step 2: Add a Builder constructor
Our builder class need a constructor just as any other class. But the only parameters that we are using this time are the required attributes. In this constructor we can also initialise our list or other important (fixed) values.
public AutoBuilder(String name){
this.name = name;
this.owners = new LinkedList();
}Step 3: Add a Builder setter for each optional attribute
For all the remaining attributes we should write a setter method in order to being able to give them a value. The only difference between this setters and a regular one is that we use our builder class as the return parameter and not void. To accomplish this we must return the our builder class after setting the value.
public AutoBuilder price(double price){
this.price = price;
return this;
}public AutoBuilder year(int year){
this.year = year;
return this;
}public AutoBuilder color(String color){
this.color = color;
return this;
}public AutoBuilder maxSpeed(double maxSpeed){
this.maxSpeed = maxSpeed;
return this;
}public AutoBuilder owners(Owner…owners){
for(Owner o: owners)
this.owners.add(o);
return this;
}
Step 4: Implement the build method
The last step for our builder class is to implement a build method. It should just return it self. But we have the option to add some extra functionality. For example in our Auto-Management software we want that all instances of the Auto class have at least one owner.
public AutoBuilder build() throws Exception{
if(owners.size() > 0){
return this;
}else{
throw new Exception(“We need at least one owner!”);
}
}Step 5: Add a class constructor
Our final step is to add a constructor to our main class. It should use only have a Builder class as input parameter and it should copy all the information of it.
public Auto(AutoBuilder builder){
this.name = builder.name;
this.price = builder.price;
this.year = builder.year;
this.color = builder.color;
this.maxSpeed = builder.maxSpeed;
this.owners = builder.owners;
}Step 6: Use it!
To create an instance of our class Auto. We just need to create a new instance of our builder and add all the properties that we want. After that we invoque our build method in other to materialise it. In this particular case we have to handle an invalid object exception because we want to create Autos with at least one Owner.
Owner o1 = new Owner(“Tony”);
Owner o2 = new Owner(“Sam”);
Owner o3 = new Owner(“Matt”);Auto auto = null;try {
auto = new Auto(new AutoBuilder(“Tesla Model S”)
.color(Color.White)
.maxSpeed(300)
.price(70000)
.year(2014)
.owners(o1,o2)
.owners(o3)
.build());
} catch (Exception e) {}
You can get the example code from my GitHub account: https://github.com/paguos/designpatterns









