-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcohesion.ts
113 lines (97 loc) · 3.11 KB
/
cohesion.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Cohesion refers to the degree to which the elements
* within a module or class belong together and have a single,
* well-defined responsibility. In other words, it refers to
* how closely related the functionalities of a module or
* class are. High cohesion means that the functions and
* properties of a module or class are strongly related
* and have a clear and well-defined purpose, whereas low
* cohesion means that the functions and properties are
* loosely related and may have multiple responsibilities.
*/
// Low cohesion:
/**
* The Vehicle class has three properties
* (engine, seats, and color) and three methods
* (drive(), paint(), and addSeats()), but they are not
* strongly related. The drive() method is responsible
* for driving the vehicle, the paint() method is responsible
* for painting the vehicle, and the addSeats() method is
* responsible for adding seats to the vehicle. These
* responsibilities could be split into separate classes or
* modules with better cohesion.
*/
class Vehicle {
engine: string;
seats: number;
color: string;
constructor(engine: string, seats: number, color: string) {
this.engine = engine;
this.seats = seats;
this.color = color;
}
drive() {
console.log(`Driving the ${this.color} vehicle with ${this.engine} engine`);
}
paint(newColor: string) {
this.color = newColor;
console.log(`Vehicle painted to ${this.color}`);
}
addSeats(newSeats: number) {
this.seats += newSeats;
console.log(`Vehicle now has ${this.seats} seats`);
}
}
// Medium cohesion:
/**
* In this example, the ShoppingCart class has four methods
* (addItem(), removeItem(), calculateTax(), and checkout())
* that are related to shopping cart functionality. However, the calculateTax()
* method could be split into a separate tax calculation module or class with
* higher cohesion.
*/
class ShoppingCart {
items: string[];
total: number;
constructor() {
this.items = [];
this.total = 0;
}
addItem(item: string, price: number) {
this.items.push(item);
this.total += price;
}
removeItem(index: number, price: number) {
this.items.splice(index, 1);
this.total -= price;
}
calculateTax() {
return this.total * 0.1;
}
checkout() {
console.log(`Items: ${this.items.join(', ')}`);
console.log(`Total: $${this.total.toFixed(2)}`);
console.log(`Tax: $${this.calculateTax().toFixed(2)}`);
}
}
// High cohesion:
/**
* In this example, the Rectangle class has two methods
* (getArea() and getPerimeter()) that are closely related to
* the rectangle geometry. These methods have a clear and
* well-defined purpose and belong together in the same class.
*/
class Rectangle {
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
getPerimeter() {
return 2 * (this.width + this.height);
}
}