An Introduction to Builder Pattern

Hasini Sandunika Silva
Nerd For Tech
Published in
4 min readMay 24, 2021
An Introduction to Builder Pattern

The builder pattern comes under the creational design pattern.

When we have multiple variables inside the class, we have to create instances of this by including more than one parameter at a time. In such cases, we can implement the constructors manually, but it is hard and time-consuming. As a solution for that, we can create multiple constructors by using the telescope method. But, here also due to passing null values, etc., has to handle each scenario (NullPointerExceptions) manually. Instead of using constructors, we can use setters to assign values to the variables declared inside the class, but here, mutability may cause some problems with the logical implementation. As a solution for all these problems, we can use the builder pattern. This provides immutability and here, no need to create constructors plus you only need to pass the parameters that you want to include inside the instance.

The following describes an example of the builder pattern using both java and JavaScript.

Assume, a bank needs to hold information about the accounts of each customer. Here, it keeps the mandatory details of each customer including, the customer’s unique account number, customer’s name. and the corresponding branch name. Apart from that, this holds the information of, type of the account, and the deposited amount as optional fields.

First, let’s consider the java implementation.

Here, I have used the AccoutTelescope1 and AccoutTelescope2 classes to provide some examples of telescope implementation. Refer to the following codes.

In Account class, I have implemented the following to inject the builder pattern properties into the code.

Here, I have implemented a static class called AccountBuilder, when you access the AccountBuilder class, it directly assigns the values for the fields, accNo, holder, city with the parameters you pass. I have provided setType(String type) and setDeposit(double deposit) methods to add the optional parameters to the static class (already created when it is accessed). Again I have implemented the build() method to return an object of Account type by instantiating it using the existing accountBuilder object of this class. Refer to the following code.

Finally, I have implemented the main method inside the MainApplication class to display the outputs through the toString() method. Here I have created objects from AccoutTelescope1 and AccoutTelescope2 classes to demonstrate how these are working with the passed parameters. Again, I have printed the outputs using the AccountBuilder and Account classes by adding the optional fields using setType(String type) and setDeposit(double deposit) methods. Refer to the following code.

The following defines the output.

From AccountTelescope1
Account No: 001-234-332
Holder: Hasini
City: Colombo
Account Type: null
Amount of Deposited: 0.0
From AccountTelescope2
Account No: 001-500-200
Holder: Hasini
City: Colombo
Account Type: Fixed-Deposit
Amount of Deposited: 0.0
Account No: 001-400-165
Holder: Hasini
City: Colombo
Account Type: Current
Amount of Deposited: 5000.0
Account No: 001-400-900
Holder: Hasini
City: Colombo
Account Type: Fixed-Deposit
Amount of Deposited: 7000.0

Now, let’s consider the JavaScript implementation for the same scenario.

As per the above code, I have created 2 instances of AccountClass class by passing some parameters to them.

The following defines the outputs.

AccountClass {
accNo: '001-234-332',
holder: 'Hasini',
city: 'Colombo',
type: undefined,
deposit: undefined
}
AccountClass {
accNo: '001-500-200',
holder: 'Hasini',
city: 'Colombo',
type: 'Fixed-Deposit',
deposit: undefined
}

This is the usual way of instantiating classes in JavaScript, but because of using the builder pattern, I have introduced another class called AcountBuilder. Refer to the following code.

Here also I have used the same logic as in java implementation to integrate the builder pattern.

The following defines the output received.

Account1 {
accNo: '001-400-165',
holder: 'Hasini',
city: 'Colombo',
type: 'Current',
deposit: 5000
}
Account1 {
accNo: '001-400-900',
holder: 'Hasini',
city: 'Colombo',
type: 'Fixed-Deposit',
deposit: 7000
}

In JavaScript, we can use another technique to implement the builder pattern without using the AcountBuilder class. Here, we can pass the optional parameters as a JSON object. Refer to the following code.

The following defines the output received.

Account1 {
accNo: '001-400-165',
holder: 'Hasini',
city: 'Colombo',
type: 'Current',
deposit: 5000
}
Account1 {
accNo: '001-400-900',
holder: 'Hasini',
city: 'Colombo',
type: 'Fixed-Deposit',
deposit: 7000
}

This is the end of the article, I hope you gained some understanding of the builder pattern and the implementations.

References

--

--