Understanding Builder Design Pattern

Build complex objects step by step

Ganesh Kumar Marimuthu
May 15 · 3 min read
Photo by Nazarii Yurkov on Unsplash

Let’s say we want to create an Employee class that has the below mandatory and optional attributes.

Mandatory Attributes

  • firstName
  • lastName
  • primaryMobileNumber
  • primaryEmail

Optional Attributes

  • secondaryMobileNumber
  • secondaryEmail

The Employee class and its constructor looks like below

The main problem with the above constructor is that we have to manually pass null values for the optional attributes that we don’t want to use.

It looks simple in our class, but consider a complex class with more mandatory and optional attributes. And we also don’t know which one is mandatory and which one is optional.

Another way to create an Employee class is to have multiple constructors for the required combination of the attributes which is even complex.

Here comes the lifesaver, Builder Design Pattern.

In Builder Pattern, the actual class relies on another class called Builder Class for object creation.

Using Builder Pattern, an object can be created using multiple steps, An Initial Step, 0 or more Intermediate Steps, and Terminal Step.

Now, we added a nested class inside the Employee class called EmployeeBuilder. The EmployeeBuilder class will be responsible for the construction of the Employee object.

All the mandatory attributes can be set via the EmployeeBuilder constructor.

All the optional attributes can be set via the public setter methods setSecondaryMobileNumber and setSecondaryEmail

Once all the attributes are set, the object can be created by calling the build method of the EmployeeBuilder class. The build method creates and returns an Employee object.

The constructor of the Employee class is made private so that it can be accessed only from the EmployeeBuilder class.

Now, the object can be created using the EmployeeBuilder class as given below

The entire source code can be found in the below gist

Thank you 🤘

To know more about me, visit ganeshkumarm.me

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Ganesh Kumar Marimuthu

Written by

Software Engineer @ Optum. ✍️ Blog Writer 🔸 👨‍💻 Full Stack Developer

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store