Factory Method Missing Return
Spot the subtle bug in this JavaScript Factory pattern that leaves callers with undefined.
Codejavascript
class Circle {
draw() { return 'drawing circle'; }
}
class Square {
draw() { return 'drawing square'; }
}
class ShapeFactory {
createShape(type) {
switch (type) {
case 'circle':
new Circle();
break;
case 'square':
return new Square();
default:
throw new Error('Unknown shape: ' + type);
}
}
}
const factory = new ShapeFactory();
const shape = factory.createShape('circle');
console.log(shape.draw());What is the bug in this Factory implementation?