-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathbuilder.dart
88 lines (72 loc) · 2.46 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
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
class PizzaBuilder {
late String _crust;
late int _diameter;
late Set<String> _toppings;
PizzaBuilder(this._diameter);
String get crust => _crust;
set crust(String newCrust) {
_crust = newCrust;
}
int get diameter => _diameter;
set diameter(int newDiameter) {
_diameter = newDiameter;
}
Set<String> get toppings => _toppings;
set toppings(Set<String> newToppings) {
_toppings = newToppings;
_ensureCheese();
}
void _ensureCheese() {
_toppings.add("cheese");
}
Pizza build() {
return Pizza(this);
}
}
class Pizza {
late String _crust;
late int _diameter;
late Set<String> _toppings;
Pizza(PizzaBuilder builder) {
_crust = builder.crust;
_diameter = builder.diameter;
_toppings = builder.toppings;
}
String get crust => _crust;
int get diameter => _diameter;
String get toppings => _stringifiedToppings();
String _stringifiedToppings() {
var stringToppings = _toppings.join(", ");
var lastComma = stringToppings.lastIndexOf(",");
var replacement =
",".allMatches(stringToppings).length > 1 ? ", and" : " and";
return stringToppings.replaceRange(lastComma, lastComma + 1, replacement);
}
@override
String toString() {
return "A delicous $_diameter\" pizza with $_crust crust covered in $toppings";
}
}
void main() {
// Create a handy PizzaBuilder with an 8" diameter.
var pizzaBuilder = PizzaBuilder(8);
// Add some attributes to the builder.
pizzaBuilder.crust = "deep dish";
pizzaBuilder.toppings = Set.from(["pepperoni"]);
// Let's make a pizza!
var plainPizza = Pizza(pizzaBuilder);
print("Behold! $plainPizza.");
assert(plainPizza.toString() ==
"Behold! A delicous 8\" pizza with deep dish crust covered in pepperoni and cheese.");
// Now to adjust some things for the next pizza...
pizzaBuilder.crust = "gold plated";
pizzaBuilder.diameter = 72;
pizzaBuilder.toppings = Set.from(["anchovies", "caviar", "diamonds"]);
// The beauty of the build is you can quickly iterate and produce instances of a class.
// For example, we have an early employee of the latest unicorn in line. So much disposable income!
// Also note, we use the .build() function of the builder this time.
var luxuriousPizza = pizzaBuilder.build();
print("Wow! $luxuriousPizza? Someone is rich!");
assert(luxuriousPizza.toString() ==
"Wow! A delicous 72\" pizza with gold plated crust covered in anchovies, caviar, diamonds, and cheese? Someone is rich!");
}