-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComposite.js
90 lines (77 loc) · 1.28 KB
/
Composite.js
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
class Equipment
{
getPrice()
{
return this.price || 0;
}
getName()
{
return this.name;
}
setName(name)
{
this.name = name;
}
setPrice(price)
{
this.price = price;
}
}
class Engine extends Equipment
{
constructor()
{
super();
this.setName('Engine');
this.setPrice(800);
}
}
class Body extends Equipment
{
constructor()
{
super();
this.setName('Body');
this.setPrice(3000);
}
}
class Tools extends Equipment
{
constructor()
{
super();
this.setName('Tools');
this.setPrice(4000);
}
}
class Composite extends Equipment
{
constructor()
{
super();
this.equipments = [];
}
add(equipment)
{
this.equipments.push(equipment);
}
getPrice()
{
return this.equipments
.map(equipment => equipment.getPrice())
.reduce((a,b) => a+b);
}
}
class Car extends Composite
{
constructor()
{
super();
this.setName('Audi');
}
}
const myCar = new Car();
myCar.add(new Engine());
myCar.add(new Body());
myCar.add(new Tools());
console.log(`${myCar.getName()} price is ${myCar.getPrice()}$`);