Member-only story
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.

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.