
Project Technical Lead
Today we will take a look at another design pattern from the category of patterns for creating objects (creational patterns) – Builder.
Read more about other design patterns:
The Builder design pattern is a software design pattern that simplifies the construction of complex objects by breaking them down into a series of steps.
It solves the problem of creating objects with a large number of parameters or optional properties. Instead of having constructors with a long list of parameters, the Builder design pattern allows us to create objects using clear steps.
The Builder pattern works by having a separate class, called the Builder, which is responsible for the step-by-step construction of an object. This class provides methods for setting individual properties of the object. Finally, when all the properties are set, we can create the actual object using the build() method.
Let’s see how to implement the Builder pattern in Java.
Product.java
In this example, we have created a Product class that represents a product. The Product class has a private constructor and a nested Builder class that sets the properties of the object incrementally.
We use the setNazov, setPopis and setCena methods to set individual properties. Finally, we call the build() method, which creates an object of type Product. Then we can use the created object and access its properties.
package designpatterns;
public class Product {
private String title;
private String description;
private double price;
// Private constructor to prevent instantiation of the object
private Product(Builder builder) {
this.title = builder.title;
this.description = builder.description;
this.price = builder.price;
}
// Getters for object attributes (title, description, price)
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
// Design Pattern Builder
public static class Builder {
private String title;
private String description;
private double price;
public Builder() {
// Default values, or we can omit the constructor and initialize values in setters
this.title = "";
this.description = "";
this.price = 0.0;
}
public Builder setTitle(String title) {
this.title = title;
return this;
}
public Builder setDescription(String description) {
this.description = description;
return this;
}
public Builder setPrice(double price) {
this.price = price;
return this;
}
public Product build() {
return new Product(this);
}
}
}
Here is an example of using the Product class with the Builder design pattern implementation:
Main.java
In this example, we have used the Builder pattern to create a Product type object, but this pattern is applicable to any object that has more complex property settings.
import designpatterns.Product;
// Example
public class Main {
public static void main(String[] args) {
Product produkt = new Product.Builder()
.setTitle("Japanese tea Gyokuro")
.setDescription("Quality shaded Japanese green tea")
.setPrice(29.99)
.build();
System.out.println("Title: " + produkt.getTitle());
System.out.println("Description: " + produkt.getDescription());
System.out.println("Price: " + produkt.getPrice());
}
}
Output
We have prepared files with the above Java Builder example in the form of code that you can run directly in Java. Download the code here.