-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposite.js
More file actions
73 lines (60 loc) · 1.3 KB
/
Composite.js
File metadata and controls
73 lines (60 loc) · 1.3 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
// Composite is a structural pattern.
// Компоновщик - структурный шаблон проектирования.
class Structure {
getName() {
return this.name || 'unnamed';
}
setName(name) {
this.name = name;
}
getPrice() {
return this.price || 0;
}
setPrice(price) {
this.price = price;
}
}
class Box extends Structure {
constructor() {
super();
this.setName('Box');
this.setPrice(700);
}
}
class Shelf extends Structure {
constructor() {
super();
this.setName('Shelf');
this.setPrice(300);
}
}
class Door extends Structure {
constructor() {
super();
this.setName('Door');
this.setPrice(200);
}
}
class Composite extends Structure {
constructor() {
super();
this.structures = [];
}
add(structure) {
this.structures.push(structure);
}
getPrice() {
return this.structures.map(structure => structure.getPrice()).reduce((a, b) => a + b);
}
}
class KitchenCabinet extends Composite {
constructor() {
super();
this.setName('Ikea');
}
}
const myKitchenCabinet = new KitchenCabinet();
myKitchenCabinet.add(new Box());
myKitchenCabinet.add(new Shelf());
myKitchenCabinet.add(new Door());
console.log(`Kitchen cabinet: ${myKitchenCabinet.getName()}, Price: $${myKitchenCabinet.getPrice()}`);