Introduction
The Builder Pattern is a design pattern that is used to separate the construction of a complex object from its representation. It is a creational design pattern that helps to create an object step by step, rather than having all the required parameters in the constructor.
The Builder pattern is useful because it:
- Separates the construction of a complex object from its representation.
- Allows for the creation of an object step by step, rather than having all the required parameters in the constructor.
- Provides a clear and consistent interface for creating objects, making the code more readable and maintainable.
- Allows for optional fields to be set in a clear and consistent way, instead of using multiple constructors or setter methods.
- Enforces immutability by ensuring that all fields are set before an object can be used.
- Simplifies the main class constructor by moving the construction process to the builder.
- Makes the code more reusable and testable by allowing different parts of the construction process to be swapped out.
The most common ways to implement the Builder pattern in Java are:
- Using a static inner class
- Using the fluent interface pattern
- Using Lombok library
Method 1: Using a static inner class
This is the most common way to implement the Builder pattern in Java. The builder class is made a static inner class of the class it is building. This allows the builder class to have access to the private fields of the class it is building.
class Person {
private final String name;
private final int age;
private final String address;
private final String phone;
private Person(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.address = builder.address;
this.phone = builder.phone;
}
public static class Builder {
private final String name;
private int age;
private String address;
private String phone;
public Builder(String name) {
this.name = name;
}
public Builder age(int age) {
this.age = age;
return this;
}
public Builder address(String address) {
this.address = address;
return this;
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public Person build() {
return new Person(this);
}
}
}
The static inner class implementation of the Builder pattern keeps the builder code close to the main class, which makes it easier to understand the relationship between the main class and the builder. It also simplifies the main class constructor as it eliminates the need for an extra class. However, this implementation has some drawbacks, as the construction process and main class are tightly coupled, which can lead to less clear separation of concerns.
Method 2: Using the fluent interface pattern
This method uses a fluent interface, which is a way of designing the API of a class so that method calls can be chained together to form a sentence-like structure. The fluent interface pattern is used to make the code more readable and expressive.
class Address {
private final String street;
private final String city;
private final String zip;
private Address(Builder builder) {
this.street = builder.street;
this.city = builder.city;
this.zip = builder.zip;
}
public static class Builder {
private String street;
private String city;
private String zip;
public Builder street(String street) {
this.street = street;
return this;
}
public Builder city(String city) {
this.city = city;
return this;
}
public Builder zip(String zip) {
this.zip = zip;
return this;
}
public Address build() {
return new Address(this);
}
}
}
class Person {
private final String name;
private final int age;
private final Address address;
private final String phone;
private Person(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.address = builder.address;
this.phone = builder.phone;
}
public static class Builder {
private final String name;
private int age;
private Address address;
private String phone;
public Builder(String name) {
this.name = name;
}
public Builder age(int age) {
this.age = age;
return this;
}
public Builder address(Address address) {
this.address = address;
return this;
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public Person build() {
return new Person(this);
}
}
}
The code for creating a Person
object is shown below:
Address address = new Address.Builder()
.street("123 Main St")
.city("Anytown")
.zip("12345")
.build();
Person person = new Person.Builder("John Smith")
.age(30)
.address(address)
.phone("555-555-5555")
.build();
You can also inline the address variable to create the Person
object in a single assignment statement:
Person person = Person.builder()
.name("John Smith")
.age(30)
.address(Address.builder()
.street("123 Main St")
.city("Anytown")
.zip("12345")
.build())
.phone("555-555-5555")
.build();
The Fluent Interface pattern allows for a more natural and readable code, as it makes the construction process feel more like a conversation. This implementation also simplifies the main class constructor and eliminates the need for an extra class. However, it can make the code harder to understand, as the construction process and main class are tightly coupled, and it’s less clear separation of concerns.
Method 3: Using Lombok library
This method uses a library called Lombok, which provides annotations that can be added to the class being built to automatically generate the builder code. This makes the code more concise and less error-prone.
import lombok.Builder;
import lombok.Data;
@Builder
class Address {
private final String street;
private final String city;
private final String zip;
}
@Builder
class Person {
private final String name;
private final int age;
private final Address address;
private final String phone;
}
The Lombok library implementation of the Builder pattern allows for concise and readable code, as it eliminates the need for extra classes and verbose setter methods. This implementation is easy to implement using annotations, and the builder class can be generated at compile-time. However, it has some drawbacks, as it requires an additional dependency and limited control over the builder’s methods, and it’s not possible to enforce immutability with this implementation.