Challenge: Solution Review
This lesson will explain the solution to the problem from the last coding challenge.
Solution #
Explanation #
You were given the following code:
var assignment = new Assignment();
var assignmentBuilder = new AssignmentBuilder("Math","hard","12th June, 2020");
var mathAssignment = assignment.make(assignmentBuilder);
mathAssignment.announcement();
You had to complete the code implementation of this program. From the first line, we know that we need to have a class Assignment that accepts no parameters. So let’s declare that first.
The code above code gives an error since some other functions/methods being used are still not defined. So let’s move on to the next line.
var assignmentBuilder = new AssignmentBuilder("Math","hard","12th June, 2020");
From the following line, we know that there is a class AssignmentBuilder which accepts the subject, the difficulty level, and the date of submission of assignment as parameters. This also means this class has these parameters defined in it, which are set when the corresponding arguments are passed to it.
Now, we get an error that the make function is not defined. Let’s understand what the make function is doing.
make is called on the assignment variable and accepts the assignmentBuilder as a parameter; meaning, it is defined in the Assignment class so let’s define it first.
We defined make and passed a builder parameter to it. In the example above, we are passing assignmentBuilder to it, which contains an object with the three properties: subject, level, and dueDate initialized to corresponding values.
From the variable name mathAssignment we can tell that the make function takes the AssignmentBuilder to create a math assignment specifically. So instead of returning an object AssignmentBuilder should return an assignment which the make will create. Let’s modify our code:
assignment should have the subject, level, and dueDate properties in it. For that to happen assignment should be an object containing these properties. How about we divide this task into two steps. Let’s create a Task class that contains these properties. An instance of this class can be stored in assignment. So let’s modify the code accordingly.
Now we need to set the properties for the assignment. So let’s define add functions for these properties in Task.
When run the code above, you get the following error:
"Cannot read property 'addSubject' of undefined"
Since Task initialized all properties to null you get this error. This is where we can utilize make. Recall make accepts an AssignmentBuilder object and creates a specific assignment. Keeping in mind that we are implementing the builder pattern we can change our code as follows:
We simply introduced steps in the form of functions in AssignmentBuilder. And then we called these steps in our make function. Next, we need to move the announcement function. From the code, we can see that it’s invoked on mathAssignment variable which is storing the output returned from the make function, i.e., the Task object. Hence, we will move it there.
And we’re done!!! The code produces the correct output.
Please note that this is just one of the ways to achieve this output using the builder pattern.
Let’s discuss the prototype pattern in the next lesson.