Created
February 7, 2023 18:14
-
-
Save wrongbyte/3e29ca925b4eba6752c3c62bf801fe5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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