In Java one of the most powerful ways to create an instance, except for dependency injection, is using the builder pattern.
The builder pattern helps to create immutable object and avoid to use long or many constructors.
The immutability in Java beans can be guaranteed defining all the attributes final, so once set, they remain the same and the status of the object never changes. As consequence, the constructor should set all the attributes and if there are a lot it can cause misleading set. This because Java constructors use only the position to identify the attributes and place the parameters in the wrong order is very easy, especially of the same type.
public class Person {
private final String name;
private final String surname;
private final Integer age;
public Person(String name, String surname, Integer age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public String getName() { return name; }
public String getSurname() { return surname; }
public Integer getAge() { return age; }
}
In this example is easy to declare the Person class with name and surname inverted.
To avoid this problem is possible to use a second class, the Builder, where the constructor is replaced with methods with meaningful names.
public class Builder {
private String name;
private String surname;
private Integer age;
public Builder withName(String name) {
this.name = name;
return this;
}
public Builder withSurname(String surname) {
this.surname = surname;
return this;
}
public Builder withAge(Integer age) {
this.age = age;
return this;
}
public Person build() {
return new Person(name, surname, age);
}
}
The Builder class contains the attributes values until the object is built and each parameter is set through a method.
Person person = new Builder()
.withName("Alessandro")
.withSurname("Simi")
.withAge(32)
.build();
The disadvantage of this solution (although is a good solution) is the Builder in itself, because requires to have another object and it doesn’t guarantee is used instead of the Person constructor.
To improve the solution the Builder can be defined as inner class of the Person table and reduce the visibility of the constructor, so the object can be created only through the Builder.
public class Person {
...
private Person(String name, String surname, Integer age) {
...
public static class Builder {
...
}
}
Still this solution has two drawbacks. First, the object can be built before all the parameters are set. Second, the Builder is not immutable.
Solving the first problem means introducing a chain of Builders where only the leaf contains the build method. This approach can, for example, divides the mandatory attributes from the optional ones and in general can be expressed through a Fluent Interface.
The immutability can be possible if every method return a new instance of the Builder, but can produce a lot of boilerplate code.
public class Person {
private final String name;
private final String surname;
private final Integer age;
private Person(String name, String surname, Integer age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public String getName() { return name; }
public Integer getAge() { return age; }
public String getSurname() { return surname; }
public static AfterName name(String name) {
return new Person(name, null, null).new AfterName();
}
public class AfterName {
public AfterAge age(Integer age) {
return new Person(name, null, age).new AfterAge();
}
}
public class AfterAge {
public Builder surname(String surname) {
return new Person(name, surname, age).new Builder();
}
}
public class Builder {
public Person build() {
return Person.this;
}
}
}
This solution defines the Builder as inner class so it can access to the attributes and create a new instance for each method call. It also forces the creation when all the properties are set.