Challenge: Solution Review
This lesson will explain the solution to the problem from the last coding challenge.
Solution #
Explanation #
To make the problem easier to understand, let’s start by converting it into a diagram.
If you follow the diagram and the code, you’ll see that there is an abstract Loans constructor function. Its purpose is to instantiate an instance of a specific loan constructor depending on the parameters given.
From the diagram above, you can see that three types of loan objects can be instantiated:
-
PersonalLoan -
StudentLoan -
HomeLoan
These loans, in turn, have different types available. In our coding challenge, we do not go into these sub-categories; however, knowing this will help you in visualizing the overall picture.
Coming back to our code. Let’s start by understanding how Loans is following an abstract pattern.
-
Inside the
Loanfunction, we first define thegetloanmethod.function Loans(){ this.getloan = function(type,amount,duration){ //code } }As you can see, it accepts the parameters
type,amount, andduration. -
Next, it defines the variable
loan, which will store an instance of a specific loan at a given time.var loan; -
Depending on the
typeof loan required, thegetloanfunction instantiates an instance of that loan. For example, iftypeequalshomeit instantiatesHomeLoaninstance.loan = new HomeLoan(amount,duration)The same goes for the
studentandpersonaltype of loans. -
In the end, the
getloanfunction returns the specificloaninstance.
Now let’s look at how the instance for a specific type of loan is created. As we discussed, we have three types, and the constructor for each is declared as follows:
function HomeLoan(amount,duration){
//code..
}
function StudentLoan(amount,duration){
//code..
}
function PersonalLoan(amount,duration){
//code..
}
As mentioned in the question, all three contain the properties amount, interest, and duration. These properties are initialized in the body of the constructor.
function HomeLoan(amount,duration){
this.amount = amount
this.interest = 0.08
this.duration = duration
}
function StudentLoan(amount,duration){
this.amount = amount
this.interest = 0.03
this.duration = duration
}
function PersonalLoan(amount,duration){
this.amount = amount
this.interest = 0.05
this.duration = duration
}
Since the interest parameter is fixed for each loan, we set its value to the specified number in the constructor.
Lastly, we define the calculateInterest method in the constructor of each loan.
this.calculateInterest = function(){
return this.amount*this.interest*this.duration
}
Using the formula provided in the question, it multiplies the amount, the interest, and the duration of the current loan instance to calculate the final interest amount.
Now that the abstract pattern has been implemented, all you have to do to create an instance of a loan.
var loan = new Loans()
var homeLoan = loan.getloan('home',3200,5)
After creating an instance of Loans. Invoke the getloan method on it with the parameter that specifies the type of loan you want. This method will take care of the rest and create the required loan. You won’t need to use the new operator to create different types of loans yourself.
Now that you have learned about the creational patterns let’s discuss structural patterns in the next lesson.