Repository navigation
Expand file tree
/
Copy pathplayground.ts
More file actions
55 lines (45 loc) · 1.94 KB
/
Copy pathplayground.ts
File metadata and controls
55 lines (45 loc) · 1.94 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
/* Not Adhering to DIP - High-level module OrderProcessor directly depends on a low-level module OrderService */
type Order = { id: number; item: string; state?: string, size?: string, color?: string }
// class OrderService {
// getOrder(orderId: number): Order {
// return {id: orderId, item: "Shoe"}
// }
// }
// class OrderProcessor {
// constructor(private orderService: OrderService) {}
// processOrder(orderId: number): void {
// const order = this.orderService.getOrder(orderId);
// order.state = "processed";
// }
// }
// const orderService = new OrderService();
// const processor = new OrderProcessor(orderService);
/*
We are trying to figure out what exactly is the limitation with this implementation
It states that OrderProcessor and OrderService are tightly coupled. Any change to the OrderService class can directly impact the OrderProcessor
*/
/*
Now, let's say you need to change the behavior of the getOrder method in the OrderService class, and check type of item before returning
Because OrderProcessor directly depends on OrderService and its getOrder method, this change in the behavior of getOrder affects the OrderProcessor class
*/
// class OrderService {
// getOrder(orderId: number, itemType: string): Order {
// if (itemType === "Shoe") {
// return { id: orderId, item: "Shoe", size: "10", color: "blue" };
// } else if (itemType === "Hat") {
// return { id: orderId, item: "Hat", size: "M", color: "black" };
// } else {
// return { id: orderId, item: "Generic", size: "M", color: "red" };
// }
// }
// }
// class OrderProcessor {
// constructor(private orderService: OrderService) {}
// processOrder(orderId: number): void {
// /* Had to add an extra param here */
// const order = this.orderService.getOrder(orderId, "Shoe");
// order.state = "processed";
// }
// }
// const orderService = new OrderService();
// const processor = new OrderProcessor(orderService);