A Complete Guide to Creational Design Patterns in Java: The Builder Pattern

Varsha Das
3 min readSep 13, 2022

--

What is a design pattern?

A software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design.

Why should I learn patterns?

Makes life simpler by not reinventing the wheel

  1. Toolbox of time-tested solutions to common problems in software design.
  2. Defines a common language between the team and organization to solve emerging issues related to technological advancements.
  3. Start understanding patterns in libraries and languages.

Classification of patterns

Creational

Structural

Behavioral

There are 5 main types of Creational Design patterns:

Builder

Prototype

Singleton

Factory

Abstract Factory

In this article, we will focus in detail on one of the Creational patterns — The Builder Design pattern.

Usage:

Helps to create complex objects step-by-step.

We need this pattern to create multiple representations of an object.

Goal — to avoid complex constructors.

Existing problem:

Ever seen a humongous constructor with multiple ‘optional’ parameters?

The long list of constructor parameters is confusing and not all of them are needed at the creation of the object.

The list can be endless and every time we create a object, we may need to pass in all of the parameters.

In the event we don’t want to pass in certain parameters, we then create multiple constructors with various combinations of parameters based on the requirement. And this slowly leads to the ‘telescoping constructor problem’.

For example:

class Person {
Person(String name, int age, String gender) { ... }
Person(String name, int age, String occupation) { ... }
Person(String name, int age, String dateOfBirth, String city, String nationality) { ... }
// ...

How can Builder pattern help?

Build objects step by step, using only those steps that you really need.

The Builder pattern suggests -

1. extract the object construction code out of its own class and moving it to separate objects called builders.

2. organize object construction into a set of steps.

3. execute a series of these steps on a builder object.

The important part is that we don’t need to call all of the steps. We can call only those steps that are necessary for producing a particular configuration of an object.

Instead of multiple constructors, we now have the flexibility to create one person object with only name, gender and city , and another with name, gender,city and date of birth and so on.

Code with an example:

We have a traditional Person class written in Java.

There is a constructor with multiple parameters, a few of which are optional.

Below is the main class to create persons.

As we can see that every time we create a Person object we have to mandatorily supply all the parameters and there is no provision to selectively add parameters to our objects(s).

Now, we will convert our constructor to a Person builder class.

Below is the updated Main class.

Now, we can see that instead of having one big constructor to initialize the object, we can now create different representations of objects by adding parameters of our choice.

  1. Person — has name, profession and hobbies set.
  2. Person2 — has name & gender set.
  3. Person3 — has name,gender,age & profession set.

Suggested books to read:

  1. Head First Design Patterns

2. Design Patterns: Elements of Reusable Object-Oriented Software

3. Head First Object-Oriented Analysis and Design

If you liked this article, please click the “clap” button 👏 a few times.

Gives me enough motivation to put out more content like this. Please share it with a friend who you think this article might help.

Follow me Varsha Das to receive my weekly articles.

Follow my Youtube channel — Code With Ease — By Varsha, where we discuss Data Structures & Algorithms.

Connect with me — Varsha Das | LinkedIn

Thanks for reading.

Happy learning! 😁

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Varsha Das
Varsha Das

Written by Varsha Das

Senior Software Engineer @Fintech | Digital Creator @Youtube | Thrive on daily excellence | ❤️ complexity -> clarity | Devoted to health and continuous growth

No responses yet

Write a response