This article is part of a series on design patterns. Following the Creational patterns, the Builder is an extremely useful design pattern for creating objects. So, what is a Builder and how do you implement one? Through this article, we hope to provide everyone with additional knowledge about the Builder pattern.

1. What is the Builder Design Pattern?

In previous articles, I introduced various design patterns in the Creational group, such as Singleton and Factory. Today, I will introduce another design pattern related to these two: the Builder pattern.

First, a little fun fact: our predecessors were quite resourceful and came up with a bunch of patterns for future generations to use, like Singleton, Factory, and now Builder, among many others.

In reality, each pattern serves a specific purpose. Singleton ensures that only one instance is created, and Factory can create multiple types of objects based on certain conditions. However, most developers often deal with objects that have numerous attributes, and creating such objects can be quite challenging. Why is it challenging? And is there a solution to this problem? The answer is the Builder pattern.

Ok, let’s go. Now, let’s analyze the “challenge” and see how the Builder pattern helps us solve these issues.

We’ll use an example based on the Fintech platform. Let’s say we are building a Core Banking system. A specific feature we need to implement is an API to create a BankAccount for SaigonTechnologyBank. Although it is an internationally renowned bank, it only requires information for each account that includes: the customer’s full name, email address, and phone number.

The requirement is quite simple, and we can come up with a solution in no time.

public class BankAccount {

    private String fullName;

    private String email;

    private String phoneNumber;

    public BankAccount(String fullName, String email, String phoneNumber){

        this.fullName = fullName;

        this.email = email;

        this.phoneNumber = phoneNumber;

    }

}

With SaigonTechnologyBank’s success, more and more other banks want to use our CoreBanking system. However, besides the common information for a BankAccount, each new bank requires additional details like citizen identification, nationality, address, occupation, family information, and more.

Now, the BankAccount class has accumulated quite a few attributes. This means that to create an object, all these attributes must be added to the constructor. Imagine a constructor with more than ten parameters. It would be exhausting, wouldn’t it?

  • It’s exhausting because there are too many parameters passed in from the client side, which is also a drawback of using the Factory method.
  • It’s tiring because you have to remember the position of each parameter.
  • It’s exhausting because many parameters are optional.

The solution is quite simple: create multiple constructors and use the one you need to create the object. However, having many constructors can be confusing. It requires programmers to remember how to use each one or to investigate them separately to choose the best.

Well, in that case, I’ll just create a constructor with the required fields. Then, I’ll use setter() to update information for the optional fields. Great idea, but this approach cannot be applied to immutable objects.

Cry me a river. This method doesn’t work, and that one doesn’t either. But luckily, our predecessors have designed a design pattern called Builder. Just hearing the name inspires confidence. This design helps us overcome the limitations of the aforementioned issues.

2. Builder Design Pattern Diagram

Builder Design Pattern Diagram

Now, let’s explore various ways to implement the Builder design pattern. The class diagram shows this. Coders can see that implementing the Builder is easy.

The AccountDirector utilizes the BankAccountBuilder to create a BankAccount. Here, let’s assume that the BankAccount has many optional fields.

3. Builder Design Pattern Implementation

Let’s use the diagram. We’ll walk through how to implement this pattern.

To simplify things, we’ll place the BankAccountBuilder class inside the BankAccount class.

public class BankAccount{

    private String bankName;     // Required field

    private String accountNumber;// Required field

    private String idNumber;     // Required field

    private String fullName;

    private String email;

    private String password;

    private String address;

    public BankAccount(String bankName, String accountNumber, String idNumber){

        this.bankName = bankName;

        this.accountNumber = accountNumber;

        this.idNumber = idNumber;

    }

    // Getter() and Setter() should be here

    public static class BankAccountBuilder {

        private String bankName;      // Required field

        private String accountNumber; // Required field

        private String idNumber;      // Required field

        private String fullName;

        private String email;

        private String password;

        private String address;

        public BankAccountBuilder(String bankName, String accountNumber, String idNumber){

            this.bankName = bankName;

            this.accountNumber = accountNumber;

            this.idNumber = idNumber;

        }

        public BankAccountBuilder withFullName(String fullName){

            this.fullName = fullName;

            return this;

        }

        public BankAccountBuilder withEmail(String email){

            this.email = email;

            return this;

        }

        public BankAccountBuilder withPassword(String password){

            this.password = password;

            return this;

        }

        public BankAccountBuilder withAddress(String address){

            this.address = address;

            return this;

        }

        public BankAccount build() {

// validation() should be here.

     BankAccount bankAccount = new BankAccount(this.bankName, this.accountNumber, this.idNumber);

            bankAccount.setFullName(this.fullName);

            bankAccount.setEmail(this.email);

            bankAccount.setPassword(this.password);

            bankAccount.setAddress(this.address);

            return bankAccount;

        }

    }

}

Here’s how the AccountDirector class will use the BankAccountBuilder. It will use it to build BankAccount objects.

public class AccountDirector{

    public static void main(String[] args){

        String bankName = “SaigonTechnologyBank”;

        String accountNumber = “STS7777777”;

        String idNumber = “7777777”;

        BankAccount account = new BankAccount.BankAccountBuilder(bankName, accountNumber, idNumber)

                .withFullName(“Wind Hero”)

                .withAddress(“MID”)

                .build();

        System.out.println(account);

    }

}

4. Pros and Cons of Builder Design Pattern

Pros of Builder

  • The first strength of the Builder pattern is that it helps us control creating objects. They can have many attributes. And, they it is easy.
  • It eliminates constructors with many parameters and reduces the number of redundant constructors.
  • Builders can assist in creating immutable objects.

StringBuilder and StringBuffer are among the many commonly used Builders provided by Java.

Cons of Builder

  • The biggest drawback of Builder is the potential for duplicate code. This is because the Builder class often needs to fully copy all of the Product class’s attributes.
  • The Builder class will become increasingly bloated as new attributes are added.

Currently, Java developers can use Lombok. It simplifies creating a Builder.

5. The Conclusion

I’d like to conclude the article here. I’ve outlined what the Builder design pattern is and how to use it. I hope to get contributions from everyone. They will help the programming community. Especially, they will improve future articles.

In conclusion, we have covered the Builder design pattern in Java well. Need Java experts? Contact Saigon Technology today. We can advise and support you on the best solutions for your project.

contact

Java Development Services

Saigon Technology's Java development team has extensive experience, having completed hundreds of projects. Our skilled Java developers are prepared to tackle any challenge across various sectors.
Learn About Our Services arrow-narrow-right.png
Content manager
Thanh (Bruce) Pham
CEO of Saigon Technology
A Member of Forbes Technology Council

Related articles

Exploring ECMAScript 2024: What’s New and Exciting?
Technologies

Exploring ECMAScript 2024: What’s New and Exciting?

ECMAScript 2024 adds new features like Object.groupBy() and Map.groupBy(), and enhances the Temporal API, to make JavaScript more robust and expressive for modern web development.
calendar 15 Nov 2024
Implement Producer/Consumer patterns using Channel in C#
Technologies

Implement Producer/Consumer patterns using Channel in C#

Channel in C# lets threads or operations share data safely and efficiently. Follow a step-by-step guide to using it and implementing patterns.
calendar 03 Oct 2024
Real-time ASP.NET with SignalR
Technologies

Real-time ASP.NET with SignalR

Today we are going to dive into SignalR for .NET. What is a real-time app, and how does SignalR work in .NET? Cover SignalR concepts and build a chat app.
calendar 19 Sep 2024
Logging And Structured Logging With Serilog The Definitive Guide
Technologies

Logging And Structured Logging With Serilog The Definitive Guide

The software industry prioritizes performance, reliability, speed, and scalability. Observability is just as important. Logging is a key part of achieving it in software.
calendar 19 Sep 2024
Demystifying MySQL Replication: From Theory to Implementation
Technologies

Demystifying MySQL Replication: From Theory to Implementation

MySQL replication copies data from a master MySQL server to one or more slave MySQL servers. This process duplicates data across multiple servers in real-time.
calendar 18 Sep 2024
Introduction to TPL Dataflow in C#
Technologies

Introduction to TPL Dataflow in C#

We wrote imperative code with primitive synchronization from day one. Microsoft has a tool to handle asynchronous operations, breaking complex tasks into smaller pieces.
calendar 13 Sep 2024
Animation in CSS
Technologies

Animation in CSS

Use CSS animation to add captivating transitions and effects to web designs. Unleash creativity as we delve into key techniques and practices.
calendar 13 Sep 2024
Automated Localization Script
Technologies

Automated Localization Script

Google Sheets and Scripts can make working with multiple languages a lot easier, especially in mobile development and front-end and back-end development.
calendar 12 Sep 2024
Flex and Grid: From zero to hero
Technologies

Flex and Grid: From zero to hero

You are assigned to implement a UI that has a complex layout. Having trouble after a week of research? Your layout’s not perfect. This blog can help.
calendar 12 Sep 2024

Want to stay updated on industry trends for your project?

We're here to support you. Reach out to us now.
Contact Message Box
Back2Top