-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcomposite.js
59 lines (49 loc) · 988 Bytes
/
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
/**
* Created by betty on 8/7/20.
*/
"use strict";
// composes zero-or-more similar objects so that they can be manipulated as one object.
// 组装模式, 分别几个objects 组装成一个object
class Equipment {
setName(name) {
this.name = name
}
setPrice(price) {
this.price = price
}
}
class HardDrive extends Equipment {
constructor() {
super();
this.setName("HardDrive")
this.setPrice(100)
}
}
class Memory extends Equipment {
constructor() {
super();
this.setName("Memory")
this.setPrice(200)
}
}
class CPU extends Equipment {
constructor() {
super();
this.setName("CPU")
this.setPrice(300)
}
}
class Cabinet {
constructor() {
this.equipments = []
}
add(equipment) {
this.equipments.push(equipment)
}
getPrice() {
return this.equipments.reduce((accumulator, current) => {
return accumulator + current.price
}, 0)
}
}
module.exports = {HardDrive, Memory, CPU, Cabinet}