Portfolio

Blog

My articles

Builder Design Pattern

Design patterns are essential tools in the world of software development, offering proven solutions to recurring problems. One such pattern is the Builder Design Pattern, which aims to simplify the construction of complex objects. In this article, we will explore the Builder Design Pattern in depth, its benefits, and provide a sample Java program to illustrate its implementation.

What is the Builder Design Pattern ?

The Builder Design Pattern is a creational design pattern that separates the construction of a complex object from its representation. It allows you to create an object step by step and customize its properties before constructing the final product. This pattern is particularly useful when dealing with objects that have a large number of optional attributes or configuration options.

The primary components of the Builder Design Pattern include:

  1. Director: This class is responsible for orchestrating the construction process using the builder.
  2. Builder: The interface or abstract class that defines the steps and methods for constructing the object.
  3. Concrete Builder: Concrete implementations of the builder interface that provide the specific construction steps and return the final object.
  4. Product: The complex object being constructed.

Benefits of the Builder Design Pattern

Using the Builder Design Pattern offers several advantages:

  1. Readability and maintainability: The pattern makes your code more readable and allows you to modify the construction process without changing the client code.

  2. Parameter flexibility: It allows you to construct objects with a wide range of parameters, including optional ones, without creating multiple constructor overloads.

  3. Immutability: You can ensure that the constructed objects are immutable by setting their properties through the builder, which can be beneficial for thread safety.

  4. Fluent API: It enables you to use a fluent and expressive API to build objects, making your code more intuitive and self-documenting.

  5. Separation of concerns: The separation of the construction logic from the final object allows you to focus on creating complex objects without worrying about the details of how they are built.

Implementing the Builder Design Pattern

Let’s demonstrate the Builder Design Pattern with a Java program that constructs a „Person" object with optional attributes such as age and address:

package Product;

public class Person {
private String name;
private int age;
private String city;

public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getCity() {
return city;
}
}
package Builder;

import Product.Person;

public interface PersonBuilder {
PersonBuilder setName(String name);
PersonBuilder setAge(int age);
PersonBuilder setCity(String address);
Person build();
}
package Builder;

import Product.Person;

public class ConcretePersonBuilder implements PersonBuilder {
private String name;
private int age;
private String city;

@Override
public PersonBuilder setName(String name) {
this.name = name;
return this;
}

@Override
public PersonBuilder setAge(int age) {
this.age = age;
return this;
}

@Override
public PersonBuilder setCity(String city) {
this.city = city;
return this;
}

@Override
public Person build() {
return new Person(name, age, city);
}
}
package Director;

import Builder.PersonBuilder;
import Product.Person;

public class PersonDirector {
public Person createPerson(PersonBuilder builder) {
return builder.build();
}
}
import Builder.ConcretePersonBuilder;
import Builder.PersonBuilder;
import Product.Person;

public class Builder {
public static void main(String[] args) {

PersonBuilder builder = new ConcretePersonBuilder();
Person person = builder.setName("Michał")
.setAge(27)
.setCity("Kraków")
.build();

System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("City: " + person.getCity());
}
}

The result of code execution is as follows:

Name: Michał
Age: 27
City: Kraków

In this example, we’ve defined a „Person" class with optional attributes and a „PersonBuilder" interface with a concrete implementation. The „Builder" class demonstrates how to use the builder to create a "Person" object step by step.

Conclusion

The Builder Design Pattern is a valuable tool for creating complex objects in a flexible and readable manner. It promotes code maintainability, parameter flexibility, and immutability while providing a clean separation of concerns. By implementing the pattern, you can simplify object construction and improve the overall quality of your code.

Web Developer

© Michał Pieróg. All Rights Reserved.

Scroll to Top