-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
client.ts
73 lines (60 loc) · 1.96 KB
/
client.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
// The Visitor Pattern Use Case Example
import AbstractCarPart from "./abstract-car-part"
import IVisitor from "./ivisitor"
class CarBody extends AbstractCarPart {
// A part of the car
}
class Engine extends AbstractCarPart {
// A part of the car
}
class Wheel extends AbstractCarPart {
// A part of the car
}
class Car extends AbstractCarPart {
// A Car with parts
#parts: AbstractCarPart[]
constructor(name: string) {
super(name)
this.#parts = [
new CarBody('Utility Body', 'ABC-123-21', 1001),
new Engine('V8 engine', 'DEF-456-21', 2555),
new Wheel('FrontLeft', 'GHI-789FL-21', 136),
new Wheel('FrontRight', 'GHI-789FR-21', 136),
new Wheel('BackLeft', 'GHI-789BL-21', 152),
new Wheel('BackRight', 'GHI-789BR-21', 152),
]
}
accept(visitor: IVisitor) {
this.#parts.forEach((part) => {
part.accept(visitor)
})
visitor.visit(this)
}
}
class PrintPartsVisitor implements IVisitor {
// Print out the part name and sku
visit(abstractCarPart: AbstractCarPart) {
if (abstractCarPart.sku !== undefined) {
console.log(
`${abstractCarPart.name}\t:${abstractCarPart.sku}\t:${abstractCarPart.price}`
)
}
}
}
class TotalPriceVisitor implements IVisitor {
// Print out the total cost of the parts in the car
totalPrice = 0
visit(abstractCarPart: AbstractCarPart) {
if (abstractCarPart.price !== undefined) {
this.totalPrice += abstractCarPart.price as number
}
}
}
// The Client
const CAR = new Car('DeLorean')
// Print out the part name and sku using the PrintPartsVisitor
CAR.accept(new PrintPartsVisitor())
// Calculate the total prince of the parts using the TotalPriceVisitor
const TOTAL_PRICE_VISITOR = new TotalPriceVisitor()
CAR.accept(TOTAL_PRICE_VISITOR)
console.log(`Total Price = ${TOTAL_PRICE_VISITOR.totalPrice}`)