builder
This commit is contained in:
parent
7959da3cd7
commit
97c5cac80f
36
builder.dart
36
builder.dart
|
@ -8,24 +8,18 @@ class PizzaBuilder {
|
||||||
PizzaBuilder(this._diameter);
|
PizzaBuilder(this._diameter);
|
||||||
|
|
||||||
String get crust => _crust;
|
String get crust => _crust;
|
||||||
set crust(String newCrust) {
|
set crust(String newCrust) => _crust = newCrust;
|
||||||
_crust = newCrust;
|
|
||||||
}
|
|
||||||
|
|
||||||
int get diameter => _diameter;
|
int get diameter => _diameter;
|
||||||
set diameter(int newDiameter) {
|
set diameter(int newDiameter) => _diameter = newDiameter;
|
||||||
_diameter = newDiameter;
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> get toppings => _toppings;
|
Set<String> get toppings => _toppings;
|
||||||
set toppings(Set<String> newToppings) {
|
set toppings(Set<String> newToppings) {
|
||||||
_toppings = newToppings;
|
_toppings = newToppings;
|
||||||
_ensureCheese();
|
_addCheese();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _ensureCheese() {
|
void _addCheese() => _toppings.add("сыр");
|
||||||
_toppings.add("cheese");
|
|
||||||
}
|
|
||||||
|
|
||||||
Pizza build() {
|
Pizza build() {
|
||||||
return Pizza(this);
|
return Pizza(this);
|
||||||
|
@ -49,32 +43,30 @@ class Pizza {
|
||||||
String _stringifiedToppings() {
|
String _stringifiedToppings() {
|
||||||
var stringToppings = _toppings.join(", ");
|
var stringToppings = _toppings.join(", ");
|
||||||
var lastComma = stringToppings.lastIndexOf(",");
|
var lastComma = stringToppings.lastIndexOf(",");
|
||||||
var replacement = ",".allMatches(stringToppings).length > 1 ? ", and" : " and";
|
var replacement = ",".allMatches(stringToppings).length > 1 ? ", и" : " и";
|
||||||
|
|
||||||
return stringToppings.replaceRange(lastComma, lastComma + 1, replacement);
|
return stringToppings.replaceRange(lastComma, lastComma + 1, replacement);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
return "A delicous $_diameter\" pizza with $_crust crust covered in $toppings";
|
return "Пицца с $_diameter\" диметром с $_crust корочкой. Ингридиенты: $toppings";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
var pizzaBuilder = PizzaBuilder(8);
|
var pizzaBuilder = PizzaBuilder(8);
|
||||||
|
|
||||||
pizzaBuilder.crust = "deep dish";
|
pizzaBuilder.crust = "хрустящей";
|
||||||
pizzaBuilder.toppings = Set.from(["pepperoni"]);
|
pizzaBuilder.toppings = Set.from(["пеперони"]);
|
||||||
|
|
||||||
var plainPizza = Pizza(pizzaBuilder);
|
var plainPizza = Pizza(pizzaBuilder);
|
||||||
print("Behold! $plainPizza.");
|
print("Заказана $plainPizza.");
|
||||||
assert(plainPizza.toString() == "Behold! A delicous 8\" pizza with deep dish crust covered in pepperoni and cheese.");
|
|
||||||
|
|
||||||
pizzaBuilder.crust = "gold plated";
|
pizzaBuilder.crust = "сырной";
|
||||||
pizzaBuilder.diameter = 72;
|
pizzaBuilder.diameter = 10;
|
||||||
pizzaBuilder.toppings = Set.from(["anchovies", "caviar", "diamonds"]);
|
pizzaBuilder.toppings = Set.from(["ананасы"]);
|
||||||
|
|
||||||
var luxuriousPizza = pizzaBuilder.build();
|
var ananasPizza = pizzaBuilder.build();
|
||||||
print("Wow! $luxuriousPizza? Someone is rich!");
|
print("Заказана $ananasPizza!");
|
||||||
assert(luxuriousPizza.toString() == "Wow! A delicous 72\" pizza with gold plated crust covered in anchovies, caviar, diamonds, and cheese? Someone is rich!");
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue