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

Avoid Telescoping constructors with the Builder pattern

--

Image by Steffi Timm from Pixabay

In some cases, multiple Constructors are needed in an application. This may be due to the user requirement. But the problem with multiple Constructors is that, if there are too many parameters, your code could get ugly( too complex). Look at the example below.

However, rather than implementing Constructors as above, some programmers use what’s known as a Telescoping Constructor.

So, here’s an example of how a Telescoping Constructor can be used. This is only one approach (In this example you can see the Constructor refer to the previous Constructor to assign values).

According to this approach, you can have multiple Constructors in a Telescoping Constructor, and each Constructor can use another Constructor based on the parameters. (See for yourself in examples below).

Here's another approach of the Telescoping Constructor, different from above.

But this is a bad approach (Using Telescoping Constructors). Because it’s a bit complicated and you can see we pass nulls here and there (There are too many nulls, we could get null pointer exception easily). So, this is the problem we are going to answer with the builder pattern.

You might think that we can use Setters, to assign variable values, instead of having multiple Constructors. But the issue with setters is that they are not immutable. As a result, when the object has been created, anyone can change it. So Setters might not be the ideal option in some situations.

So let’s see how we can come up with a solution for this problem step by step. We are taking an exam scenario of “how an employee's monthly salary is created”. In monthly salary, we have variables like employee name, employee email, basic salary, motor car allowance and bonus.

First, we put all those variables into MonthlySalary class and we add another static inner class called builder into MonthlySalary class.

Since we have two “must-have” parameters (empId, basicSalary) we will pass them as arguments to the Constructor. So, in the object creation, they will always be passed ( As below).

  // required parameter
public MonthlySalaryBuilder(String empId, String basicSalary) {
this.empId = empId;
this.basicSalary = basicSalary;
}

Now, let's create some methods to assign values to other variables. And let's make our variables of MonthlySalary class final (so they are immutable).

Ex:

  //set value of employee email
public MonthlySalaryBuilder addEmpEmail(String empEmail) {
this.empEmail = empEmail;
return this;
}

Well, now we are almost done. To finalize everything, add a method named build() to return an instance of MonthlySalary and a toString() .

Output:

Employee Salary (telescoping-1) - Id: EMP-TC-1 Email: null Basic salary: null Motor car allowance: null Bonus: nullEmployee Salary (telescoping-2) - Id: EMP-TC-2 Email: null Basic salary: null Motor car allowance: null Bonus: nullEmployee Salary (Builder) - Id: EMP0001 Email: johnDoe@gmail.com Basic salary: 999.0 Motor car allowance: 250.0 Bonus: null

Well now you can see, we get the same result with the Builder pattern, but we don’t use multiple Constructors, Any Setters and also our attributes are immutable. Further, we don’t need to pass any null values. Assume that you have a constructor with 10 parameters and you want to pass once one at the movement. For the rest of the parameters, you will have to pass 9 null values.

However, one of the drawbacks is initially you have to do some coding implementing the Builder pattern.

I hope you got the concept of the Builder pattern. If you are looking for the above codes above I have saved them in my repo here.

--

--

Nerd For Tech
Nerd For Tech

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

Responses (1)