JSFiddle - React, Tailwind, and code Playground
JavaScript
const Shapes = Object.freeze({
SQUARE: Symbol.for('Square'),
CIRCLE: Symbol.for('Circle'),
TRIANGLE: Symbol.for('Triangle')
});
class AbstractShape {
constructor(type) {
this.type = Symbol.keyFor(type);
}
getType() {
console.log(`I am a ${this.type}`);
}
}
class Square extends AbstractShape {
constructor(type) {
super(type);
this.sides = 4;
}
getDescription() {
console.log(`I have ${this.sides} sides`);
}
}
class ShapeFactory {
static issue(type) {
switch(type) {
case Shapes.SQUARE: return new Square(type);
break;
case Shapes.CIRCLE: /* same pattern with a Circle class */
break;
}
}
}
let shape = ShapeFactory.issue(Shapes.SQUARE);
shape.getType(); /* I am a Square */
shape.getDescription(); /* I have 4 sides */