Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Builder pattern is incredibly useful when you need to create objects that have many interlinking

parts or many optional and required fields. In our example here, we have two classes: an phone
class and a user class. The user is the object that we want to create dynamically using a builder.

So this user can have a name. last name, email, and a phone. This phone can have a code country
and a number. Now you can imagine that this user object could be much more complex. It could
have address, passwords, authentication tokens, and the phone could be more complex with code
area and all that extra stuff.

In the example here, we have a user, and really the only field that needs to be supplied is the
name. last name, email, and phone are all really optional fields that the user can or may not have.
If I run this real quick, you'll see that we get a user printed out with the name of David, and their
address, age, and phone are all undefined, which is perfect.

But what say we want to add a phone to our user, but we don't want to add an last name or a
email? Well, now we need to go into this user object, we need to pass in undefined both times
here for the last name, and then again, we need to pass an undefined for the email. And then
finally, we can create a new phone object, and let's just say we want to give it a code country of
one and a number of 3003291238. If we print this we will realize that it prints the user with name
and phone number but last name and email in undefined
But this is kind of complex, this is really not very self-explanatory. If all you saw was this new user
line, you don't know what undefined is setting. You don't know this is the age or that this is the
phone number. So it's confusing to figure out what you're actually setting, and you always have to
write out these undefined and make sure everything's in the correct order in order to get this to
work properly. And if you can imagine you have any parameters to create a new user, this new
user statement can become incredibly long and filled with many different undefined that are
confusing to you, the programmer, as to what they actually mean. And that's where the Builder
pattern comes in.

We are going to make the builder to build the object dynamically, the name property will continue
to be mandatory, and the rest is optional. for this we are going to change the 3 optional
parameters for a json object in such a case that the json object is empty it is as if they did not send
these parameters. For this example of how the builder works, we are going to define all the user
options except last name in this way, we verify that it is not necessary to define all the properties
of the object.

We can also make default values in the object's construction, giving a value to the property in the
constructor definition. The builder helps us build objects more easily and in fewer lines of code
and makes the code more understandable.

This pattern works as if we had set and get methods to update the already created object as well.

now we can include any optional parameters we want without filling in all the other parameters
and without having to know the order of the parameters.

You might also like