Builder Design Pattern

December 28, 2022

Builder Design Pattern

Builder Design Pattern is a creational design pattern. It is used for creating complex objects by isolating construction process from the representation. By doing this, same construction process can create different representations. Also, implementation of construction is decoupled from the representation by using builder design pattern.

Builder Design Pattern

So, when we have a complex class, even made up with other classes, it will be good to use builder design pattern rather than the old-school constructor usages. Also, with the builder design pattern, we can hide how we are construct that object from the client, so we can ensure the encapsulation of that class.

Below are the main advantages of builder design pattern;

  • Provides modularity
  • Easy to modify & maintain
  • Improves readability of the code
  • Provides decoupling

And of course below points can be assumed as the disadvantages of builder design pattern;

  • May be a little bit complex to build up the structure
  • May have performance concerns as it increases the class forms

So, let’s continue with an example for builder design pattern. My example will be with Java and you can find the repo as public in my Github profile under java-builder-design-pattern-example repo.

I will go over three options how to create a builder pattern in Java.

  • The first one is directly construct builder within the related class
  • The second one is to define a separate builder class
  • The third one is the Lombok annotation, @Builder

Let’s start with the first one, construct the builder within the related class. As shown in the below code snippet, we have a class named PersonWithBuilder which consists of four attributes. There are no setters or constructor with parameters for this class, just a constructor for the PersonBuilder class to build the class. We need to construct this class by using the PersonBuilder class and the methods in this static class. This PersonBuilder class has the attributes that we are going to build within the original class.

Please check the Gist to see how I constructed the Builder within the related class.


Second example is with a separate builder class. It has the same logic in the first example, except this class is a separate class than the class that we are going to built. Our main class, Person, has four attributes and their setters defined within the class.

Please check the Gist to see how I constructed the Builder with a separate Builder Class.


The third option is more easy, even the easiest one, Lombok’s @Builder annotation. I will not give an example for this case as a detailed example and definition can be found within the Lombok’s @Builder documentation page.

In a Nutshell

Builder Design Pattern is used for creating complex objects by isolating construction process from the representation. It is a creational design pattern and would be better to use it with complex classes.

The all codebase can be checked from the repo as public in my Github profile under java-builder-design-pattern-example repo.

Share: