-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuilderPattern.js
More file actions
125 lines (107 loc) · 2.21 KB
/
Copy pathBuilderPattern.js
File metadata and controls
125 lines (107 loc) · 2.21 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
/**
* Builder Pattern - Breaking down the creation of an object (usually complex) into smaller
* builds inside a builder. This pattern is useful if you want the creation of the components
* to be separated from the main build as well as hiding the processes from the client.
* The builder is the only one knowing the processes.
*
* Example - To brew a coffee, there are several steps. To make a good coffee you would need a
* barista that knows what he/she is doing. The barista is the builder.
*
*/
class Coffee {
constructor() {
this.expresso = 0;
this.milk = 0;
this.water = 0;
this.chocolate = 0;
}
}
class CoffeeMaker {
constructor() {
this.coffee = new Coffee();
}
putExpresso(percentage) {
this.coffee.expresso = percentage;
}
putMilk(percentage) {
this.coffee.milk = percentage;
}
putWater(percentage) {
this.coffee.water = percentage;
}
putChocolate(percentage) {
this.coffee.chocolate = percentage;
}
}
class AmericanoMaker extends CoffeeMaker {
putExpresso() {
super.putExpresso(20);
}
putMilk() {
super.putMilk(0);
}
putWater() {
super.putWater(80);
}
putChocolate() {
super.putChocolate(0);
}
}
class LatteMaker extends CoffeeMaker {
putExpresso() {
super.putExpresso(25);
}
putMilk() {
super.putMilk(75);
}
putWater() {
super.putWater(0);
}
putChocolate() {
super.putChocolate(0);
}
}
class MochaMaker extends CoffeeMaker {
putExpresso() {
super.putExpresso(25);
}
putMilk() {
super.putMilk(25);
}
putWater() {
super.putWater(0);
}
putChocolate() {
super.putChocolate(50);
}
}
class Barista {
constructor(maker) {
this.maker = maker;
}
setMaker(maker) {
this.maker = maker;
}
serveCoffee() {
return this.maker.coffee;
}
makeCoffee() {
this.maker.putExpresso();
this.maker.putMilk();
this.maker.putWater();
this.maker.putChocolate();
}
}
var americanoMaker = new AmericanoMaker();
var latteMaker = new LatteMaker();
var mochaMaker = new MochaMaker();
var john = new Barista(americanoMaker);
john.makeCoffee();
console.log(john.serveCoffee());
john.setMaker(latteMaker);
john.makeCoffee();
console.log(john.serveCoffee());
john.setMaker(mochaMaker);
john.makeCoffee();
console.log(john.serveCoffee());