2022-03-29 09:52:34 +03:00
// предоставляет способ создания составного объекта
2022-03-01 10:30:23 +03:00
class PizzaBuilder {
late String _crust ;
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 ( ) {
var pizzaBuilder = PizzaBuilder ( 8 ) ;
pizzaBuilder . crust = " deep dish " ;
pizzaBuilder . toppings = Set . from ( [ " pepperoni " ] ) ;
var plainPizza = Pizza ( pizzaBuilder ) ;
print ( " Behold! $ plainPizza . " ) ;
assert ( plainPizza . toString ( ) = = " Behold! A delicous 8 \" pizza with deep dish crust covered in pepperoni and cheese. " ) ;
pizzaBuilder . crust = " gold plated " ;
pizzaBuilder . diameter = 72 ;
pizzaBuilder . toppings = Set . from ( [ " anchovies " , " caviar " , " diamonds " ] ) ;
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! " ) ;
}