Nerd For Tech
Published in

Nerd For Tech

Understanding Builder Design Pattern

Build complex objects step by step

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.

Builder pattern builds a complex object using simple objects and using a step by step approach
- tutorialspoint

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

--

--

--

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/.

Recommended from Medium

Avail Excellent Cloud Hosting of HTS Hosting

Deploy a WordPress Blog on Akash Network

Linkerd Service Mesh By Example

SSO IdentityServer and Oracle APEX Administration

My experience as a speaker for Women Who Code

FlutterForce — #Week 144

I built a tool to Crosspost my blog to Dev.to, Medium, LinkedIn and Twitter in one click

VPS Hosting and Shared Hosting: Similarities and Differences

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
Ganesh Kumar Marimuthu

Ganesh Kumar Marimuthu

SDE II at Amazon. ✍️ Content Writer 🔸 👨‍💻 Full Stack Engineer

More from Medium

Unit Testing Concepts & Its Implementation in Java with PowerMock (and Mockito)

Software Design Patterns for Best Practices

What Is Spring Framework and How It Works

7 Reasons Java is Perfect for Enterprise Software