- [Instructor] In the last lesson,
we took a look at how using a builder can improve our code
by eliminating the telescoping constructor anti-pattern.
In this lesson, we're actually going to construct
the person builder class.
So I'm currently within the exercise files
under chapter two, chapter two lesson eight,
and inside of the start folder I'm going to create a new file
called PersonBuilder.
And inside of this file we still want to construct people
using the person class,
so I'm going to include that class.
So what we're going to do here is we're going to create a class
called PersonBuilder,
and we're going to make sure that we export this class.
All right.
So before we create the person builder,
we actually have to modify the person class
to use our builder.
So I'm going to go into the person class,
and what we're going to do here is,
instead of having a bunch of arguments
be sent to the constructor,
we're going to send the constructor one object,
and that object we're going to call the builder.
So when creating an instance of a person,
we will send the constructor the builder object,
and that's where these arguments are going to be defined.
So we'll get the name from the builder.name.
We'll get whether they're an employee
from the builder.isEmployee.
Builder.isManager.
The builder will have the hours.
And if the builder doesn't have the hours,
I'm going to optionally put a zero there.
The builder will have the money.
And again, if we don't send the money,
we'll do an optional zero for money.
And again, we'll do the same thing for shopping list.
So we'll get the shopping list from the builder.
Or we'll just make it an empty array.
Great.
So we solved the telescoping constructor problem
within our class itself.
Let's go ahead and construct the builder
so that our code will work.
So within the person builder,
this is a class,
so we're going to go ahead and define the constructor.
Now when we use the person builder
the constructor takes in the name
of the person that we're building.
And what we're going to do is we're going to say
this instance is .name is equal to the name
that was sent to the constructor.
Now that's the only argument
that we're going to set up in the constructor.
Every other modification of the person
is going to come through methods.
So if we want to make the person an employee,
we're going to invoke this makeEmployee method.
And what we'll do is we'll take this instance of the person
and we'll go ahead and say that they are an employee.
And this is really key to allowing for the chaining.
We need to return this instance.
So we've modified the current instance,
but we also need to return it
so that we can chain on and add other functions
when we're building the object.
If we actually look at the index.js file,
we can see that we have chained on makeEmployee,
.makePartTime, .build.
And that only works because we're returning this.
So the next method that we're going to add is makeManager.
And this method will take in in hours.
We'll make it set to 40,
even though we create Sue with 60 hours,
so we were able to pass that variable there.
And I can say that this instance is a manager
as well as this instance has hours
that were sent to this function.
And we also need to return this.
Great.
So now we've added the makeManager method.
Let me try this again.
Let's go ahead and add the makePartTime
and makePartTime will also have an hours.
We'll set part time hours to 20.
And when we have a part time employee
that will only affect the hours.
And again, we're going to return this.
And we also need to add a couple more things,
withMoney, and we will expect the money
is sent to this method.
So we can set this.money to the money
that is sent to this method.
And again we need to return this.
And we need to do the same thing with the shopping list,
withList.
And we'll set the list to be an empty array.
And this.shoppingList will equal the list
that was sent to this method.
And return this.
So basically we've created all of these builder methods
that are used to create a person.
The only one that we haven't created
is the one that we chain onto the end of every builder call,
which is the build method.
Now the build method is expected to actually return
a new instance of the person object.
So let's go ahead and add that method.
So whenever we build, what we're going to do
is this object is the builder.
So if we take a look at the person.js file,
all we need to do is send the builder object
to the constructor for the person instance.
So all I need to do inside of this build method
is return a new instance of a person.
And I can send all of the arguments at once,
just by sending this builder instance.
Great.
So now we've constructed our person builder.
Let's go ahead and see if our person builder works.
One thing that I'm going to do is go into the index.js
and just do some console logs.
So Charles.toString,
and we'll also go ahead and log sue.toString.
There we go.
And then I will go into the terminal,
and we can check to see if this runs
by running our index.js file.
Woops, and it looks like we have a problem.
And it looks like we have a problem
where I've created Sue, I did not spell manager correctly.
M-A-N-A-G-E-R.
So I can make that correction, save the file.
Let's go ahead and try this again.
I will clear the screen.
Node index.js.
And here we see that we've created two instances of people.
The first instance, Sue, is an employee,
is a manager, has 60 hours a week.
Unfortunately Sue doesn't have any money right now.
So the second instance that we've created is Charles.
Charles doesn't work.
Charles has 500 dollars
and he needs to buy jeans and sunglasses.
So this is an example of how we can
implement the builder pattern
and improve our code by using it.