Search⌘ K
AI Features

Challenge: Solution Review

Explore the solution to a challenge implementing the factory pattern in JavaScript. Understand how to use constructor functions like ToyDuck and ToyCar within a ToyFactory to create objects based on provided parameters. This lesson helps you apply creational design patterns to efficiently manage object instantiation in coding interviews.

We'll cover the following...

Solution

Node.js
function ToyFactory() {
this.toy = ToyDuck;
this.createToy = function(toyChosen) {
if (toyChosen.toyType == "duck") {
this.toy = ToyDuck;
} else if (toyChosen.toyType == "car") {
this.toy = ToyCar;
}
return new this.toy(toyChosen);
}
}
function ToyDuck(toyObj) {
this.color = toyObj.color;
this.price = toyObj.price;
}
function ToyCar(toyObj) {
this.color = toyObj.color;
this.price = toyObj.price;
this.name = toyObj.name;
}
var toyFactory = new ToyFactory();
var car = toyFactory.createToy({
toyType: "car",
color: "blue",
price: 12,
name: "honda"
})
var car = toyFactory.createToy({
toyType: "car",
color: "blue",
price: 12,
name: "honda"
})
console.log(car)
console.log(car instanceof ToyCar)
var duck = toyFactory.createToy({
toyType: "duck",
color: "yellow",
price: 5,
})
console.log(duck)
console.log(duck instanceof ToyDuck)

Explanation

The task was ...