MakeUseOf logo

What Is the Builder Design Pattern?

A builder holding two pieces of wood together with a metal clamp
https://unsplash.com/photos/PVGYTu5yAAA
Kadeisha is a Full-Stack Software Developer and Technical/Technology Writer. She has a Bachelor of Science in Computing, from the University of Technology in Jamaica.
Sign in to your MakeUseOf account

The Builder design pattern encapsulates the properties of an object and allows you to construct a representation of it. This pattern lets you construct complex objects, with different properties even though they belong to the same class.

A typical object class contains attributes, constructors, and methods. The builder pattern allows you to extract the construction logic from the object class and place it into classes called builders. You can then use these builder classes to create different variations of the same object.

Implementing the Builder Pattern in Java

A good example of the builder pattern is a pizza ordering system that allows customers to select different topping options.

The Product Class

One approach is to make the builder an interface, but you will need to create a product class first. The product in this sample application is a pizza.

public class Pizza {
 // properties
 private int orderNumber;
 private String pizzaDough;
 private String topping;
 
 // getters and setters
 public int getOrderNumber() {
 return orderNumber;
 }
 
 public void setOrderNumber(int orderNumber) {
 this.orderNumber = orderNumber;
 }
 
 public String getPizzaDough() {
 return pizzaDough;
 }
 
 public void setPizzaDough(String pizzaDough) {
 this.pizzaDough = pizzaDough;
 }
 
 public String getTopping() {
 return topping;
 }
 
 public void setTopping(String topping) {
 this.topping = topping;
 }
}

The Builder Class

The Pizza Java class has three properties and their respective getters and setters, but there are no constructor methods. The builder interface will allow you to create each aspect of the pizza object separately. It will then allow you to retrieve the entire pizza object.

public interface Builder {
 public void createPizzaDough();
 public void createTopping();
 public Pizza getPizza();
}

The sample pizza application allows customers to order any toppings, such as cheese, pepperoni, onion, or different combinations. Therefore, one pizza that a customer will order is cheese.

public class CheesePizzaBuilder implements Builder {
 private Pizza pizza;
 
 public CheesePizzaBuilder() {
 this.pizza = new Pizza();
 }
 
 @Override
 public void createPizzaDough() {
 this.pizza.setPizzaDough("Dough");
 }
 
 @Override
 public void createTopping() {
 this.pizza.setTopping("Cheese");
 }
 
 @Override
 public Pizza getPizza() {
 return this.pizza;
 }
}

The CheesePizzaBuilder class implements the Builder interface and uses it to create a new cheese pizza. This is one representation of the Pizza object. It also does this in a way that is independent of the Pizza class.

The CheesePizzaBuilder class doesn’t know much about the Pizza class, it only knows what it needs to know to complete its function. It knows that the Pizza class has a dough and a topping property, and it sets these properties to two specific values that every cheese pizza will have. Now every time the application calls the CheesePizzaBuilder class it will create a new Pizza that has cheese topping.

The Director Class

The director class is a crucial aspect of the builder pattern. The sole purpose of a concrete builder class is to create a specific object. It achieves this by creating the different parts of an object separately.

However, the builder concrete classes are unaware of the algorithm. None of the builder classes knows to build the dough before adding the topping. This is the function of the director class.

public class Director {
 private Builder pizzaBuilder;
 
 public Director(Builder pizzaBuilder) {
 this.pizzaBuilder = pizzaBuilder;
 }
 
 public Pizza getPizza() {
 return this.pizzaBuilder.getPizza();
 }
 
 public void makePizza() {
 this.pizzaBuilder.createPizzaDough();
 this.pizzaBuilder.createTopping();
 }
}

The Director class uses the builder interface to make pizzas. It is the keeper of the algorithm.

The Advantages of Using the Builder Design Pattern

The major advantage of using the builder design pattern is its encapsulation property. This is a crucial aspect of software engineering, as it aids in the development of secure applications.

Another advantage of this design pattern is its object construction approach. It lets you create multistep processes, where each step is independent, which makes debugging easier.

AltStyle によって変換されたページ (->オリジナル) /