-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaceSegregationViolates.ts
More file actions
73 lines (66 loc) · 2.22 KB
/
Copy pathinterfaceSegregationViolates.ts
File metadata and controls
73 lines (66 loc) · 2.22 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Interface Segregation Principle - VIOLATION Example
*
* The Interface Segregation Principle states that clients should not be forced
* to depend on interfaces they do not use. In other words, it's better to have
* multiple specific interfaces than one general-purpose interface.
*
* This example demonstrates a VIOLATION of ISP by forcing 2D shapes to implement
* a volume() method that is not applicable to them.
*/
/**
* Abstract Shape class that forces ALL shapes to implement both area() and volume()
* PROBLEM: This violates ISP because 2D shapes (Square, Rectangle) don't have volume,
* yet they are forced to implement the volume() method.
*/
abstract class Shape {
abstract area():number;
abstract volume():number; // This method doesn't make sense for 2D shapes!
}
/**
* Square is a 2D shape that should only need an area() method
* VIOLATION: Forced to implement volume() even though it's not applicable
*/
class Square extends Shape {
public area(): number {
return 0
}
/**
* This method should NOT exist for a 2D shape!
* We're forced to throw an error because the Shape interface requires it.
* This is a clear sign of Interface Segregation Principle violation.
*/
public volume(): number {
throw new Error("Square Does not have volume");
}
}
/**
* Rectangle is a 2D shape that should only need an area() method
* VIOLATION: Forced to implement volume() even though it's not applicable
*/
class Rectangle extends Shape {
public area(): number {
return 0
}
/**
* This method should NOT exist for a 2D shape!
* We're forced to throw an error because the Shape interface requires it.
* This is a clear sign of Interface Segregation Principle violation.
*/
public volume(): number {
throw new Error("Rectangle does not have volume")
}
}
/**
* Cube is a 3D shape that legitimately needs both area() and volume() methods
* This class works fine with the current interface, but the interface design
* is still problematic because it forces 2D shapes to implement volume().
*/
class Cube extends Shape {
public area() : number {
return 0
}
public volume(): number {
return 0
}
}