-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilder.js
More file actions
63 lines (52 loc) · 1.1 KB
/
Builder.js
File metadata and controls
63 lines (52 loc) · 1.1 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
// Builder is a creational pattern.
// Строитель - порождающий шаблон проектирования.
class Chocolate {
constructor() {
this.sugar = false;
this.nuts = false;
this.milk = false;
this.price = '$0';
}
}
class ChocolateBuilder {
constructor() {
this.chocolate = new Chocolate();
}
addSugar(sugar) {
this.chocolate.sugar = sugar;
return this;
}
addNuts(nuts) {
this.chocolate.nuts = nuts;
return this;
}
addMilk(milk) {
this.chocolate.milk = milk;
return this;
}
addPrice(price) {
this.chocolate.price = price;
return this;
}
madeIn(madeIn) {
this.chocolate.madeIn = madeIn;
return this;
}
build() {
return this.chocolate;
}
}
// Создаём и конфигурируем экземпляры
const chocolate1 = new ChocolateBuilder()
.addMilk(true)
.addNuts(true)
.addSugar(true)
.addPrice('$25')
.madeIn('Swiss')
.build();
const chocolate2 = new ChocolateBuilder()
.addNuts(true)
.addPrice('$15')
.build();
console.log(chocolate1);
console.log(chocolate2);