Challenge: Factory Pattern

In this challenge, you have to implement the factory pattern to solve the given problem.

Problem statement

In this problem, you need to implement a factory ToyFactory that can create a toy duck and a toy car object using either the ToyDuck or ToyCar function constructor.

A ToyDuck object should have the following properties:

  • color

  • price

A ToyCar object should have the following properties:

  • color

  • price

  • name

As you can see on line 2

this.toy = ToyDuck;

by default, the toy is of ToyDuck class.

Your task is to create a function, createToy. It should decide which toy to create depending on the parameter passed to it.

Input

createToy function called with different parameters

Output

Toy duck or car object created depending on the inputs

Sample input

var toyFactory = new ToyFactory();
var car = toyFactory.createToy({
            toyType : "car",
            color: "blue",
            price : 12,
            name : "honda"
})

Sample output

ToyCar { color: 'blue', price: 12, name: 'honda' }

Challenge #

Take a close look and design a step-by-step solution before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided. Good Luck!

function ToyFactory() {
this.toy = ToyDuck; //toy property set to ToyDuck by default
}
function ToyDuck() {} //define the ToyDuck class
function ToyCar() {} //define the ToyCar class

Let’s discuss the solution in the next lesson.