-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCP.ts
40 lines (34 loc) · 848 Bytes
/
OCP.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Open Close Principle
// The Shape class is open to extension but closed to modification.
class Shape {
area() {
throw new Error("Area method must be implemented");
}
}
// This means that it is possible to create new shapes derived from Shape, without having to change the Shape class.
// Like Circle and Rectangle
class Circle extends Shape {
radius: number;
constructor(radius: number) {
super();
this.radius = radius;
}
area() {
return Math.PI * this.radius * this.radius;
}
}
class Rectangle extends Shape {
width: number;
height: number;
constructor(width: number, height: number) {
super();
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
}
const shapes: number[] = [new Circle(2), new Rectangle(2, 3)].map((shape) =>
shape.area()
);