Code QuizIntermediate Simple Factory: centralizing object creation in a method
A Simple Factory centralizes object creation in one method, but forgetting to return the created object breaks it.
Codejavascript
class Dog { speak() { return 'Woof'; } }
class Cat { speak() { return 'Meow'; } }
class AnimalFactory {
static createAnimal(type) {
if (type === 'dog') {
new Dog();
} else if (type === 'cat') {
new Cat();
}
}
}
const pet = AnimalFactory.createAnimal('dog');
console.log(pet.speak());What is the bug in this Simple Factory code?