-
Notifications
You must be signed in to change notification settings - Fork 69
/
facade.js
55 lines (47 loc) · 1.31 KB
/
facade.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
class RecipeBook {
static findFromDish(dishName) {
let recipe;
switch (dishName) {
case 'pizza':
recipe = { name: 'pizza', procedure: ['a long procedure for the Chef'] };
break;
case 'carbonara':
recipe = { name: 'carbonara', procedure: ['a long procedure for the Chef'] };
break;
}
return recipe;
}
}
class Assistant {
static getIngredients(recipe) {
let ingredients;
switch (recipe.name) {
case 'pizza':
ingredients = ['mozzarella', 'tomato purees', 'dough', 'salt'];
break;
case 'carbonara':
ingredients = ['cheek lard', 'spaghetti', 'egg yolks', 'salt', 'pepper', 'pecorino'];
break;
}
return ingredients;
}
}
class Chef {
static prepareDish(recipe, ingredients) {
// long procedure to produce the final dish
return new Promise(resolve => {
setTimeout(() => resolve({ recipe, ingredients }), 1000);
});
}
}
// Kitchen facade
class Kitchen {
static async makeFood(dishName) {
const recipe = RecipeBook.findFromDish(dishName);
if (!recipe) throw new Error('Cannot find any recipe associated to this dish');
const ingredients = Assistant.getIngredients(recipe);
const dish = await Chef.prepareDish(recipe, ingredients);
return dish;
}
}
module.exports = Kitchen;