-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.dart
59 lines (47 loc) · 1.18 KB
/
builder.dart
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
class Pizza {
String? dough;
String? sauce;
String? topping;
String getDescription() {
return 'Pizza with $dough dough, $sauce sauce, and $topping topping.';
}
}
abstract class PizzaBuilder {
void setDough(String dough);
void setSauce(String sauce);
void setTopping(String topping);
}
class ItalianPizzaBuilder implements PizzaBuilder {
final Pizza _pizza;
ItalianPizzaBuilder() : _pizza = Pizza();
@override
void setDough(String dough) {
_pizza.dough = dough;
}
@override
void setSauce(String sauce) {
_pizza.sauce = sauce;
}
@override
void setTopping(String topping) {
_pizza.topping = topping;
}
Pizza build() {
return _pizza;
}
}
class PizzaDirector {
void makeItalianPizza(PizzaBuilder builder) {
builder.setDough('thin crust');
builder.setSauce('marinara');
builder.setTopping('mozzarella and basil');
}
}
void main() {
PizzaDirector director = PizzaDirector();
ItalianPizzaBuilder builder = ItalianPizzaBuilder();
director.makeItalianPizza(builder);
Pizza pizza = builder.build();
print(pizza.getDescription());
// Output: Pizza with thin crust dough, marinara sauce, and mozzarella and basil topping.
}