Skip to content

Instantly share code, notes, and snippets.

@wrongbyte
Created February 7, 2023 18:14
Show Gist options
  • Save wrongbyte/3e29ca925b4eba6752c3c62bf801fe5f to your computer and use it in GitHub Desktop.
Save wrongbyte/3e29ca925b4eba6752c3c62bf801fe5f to your computer and use it in GitHub Desktop.
class Circle {
radius: number;
constructor(radius: number) {
this.radius = radius;
}
}
class Square {
sideLength: number;
constructor(sideLength: number) {
this.sideLength = sideLength;
}
}
enum ShapeType {
Circle,
Square,
}
class ShapeFactory {
static createShape(type: ShapeType, param: number): Circle | Square {
switch (type) {
case ShapeType.Circle:
return new Circle(param);
case ShapeType.Square:
return new Square(param);
}
}
}
const circle = ShapeFactory.createShape(ShapeType.Circle, 10);
console.log(circle.radius); // 10
const square = ShapeFactory.createShape(ShapeType.Square, 20);
console.log(square.sideLength); // 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment