patterns/abstract_factory.dart

56 lines
965 B
Dart
Raw Normal View History

2022-03-01 10:30:23 +03:00
abstract class Drinkable {
void pour();
}
2022-03-18 14:36:55 +03:00
class Lemonade implements Drinkable {
2022-03-01 10:30:23 +03:00
void pour() {
2022-03-18 14:36:55 +03:00
print("Пью холодный лемонад!");
2022-03-01 10:30:23 +03:00
}
}
class Coffee implements Drinkable {
void pour() {
2022-03-18 14:36:55 +03:00
print("Пью горячий шоклад!");
2022-03-01 10:30:23 +03:00
}
}
abstract class DrinkFactory {
Drinkable createDrink();
DrinkFactory();
}
2022-03-18 14:36:55 +03:00
class Cafe implements DrinkFactory {
2022-03-01 10:30:23 +03:00
Drinkable createDrink() {
2022-03-18 14:36:55 +03:00
return Lemonade();
2022-03-01 10:30:23 +03:00
}
}
class CoffeeShop implements DrinkFactory {
Drinkable createDrink() {
return Coffee();
}
}
void main() {
2022-03-18 14:36:55 +03:00
var temperature = "cold";
2022-03-01 10:30:23 +03:00
late DrinkFactory destination;
2022-03-18 14:36:55 +03:00
switch (temperature) {
case "cold":
2022-03-01 10:30:23 +03:00
destination = CoffeeShop();
break;
2022-03-18 14:36:55 +03:00
case "warm":
destination = Cafe();
2022-03-01 10:30:23 +03:00
break;
default:
2022-03-18 14:36:55 +03:00
print("Мне ни холодно, ни жарко...");
2022-03-01 10:30:23 +03:00
}
2022-03-18 14:36:55 +03:00
var drinkable = destination.createDrink();
drinkable.pour();
2022-03-01 10:30:23 +03:00
/*
2022-03-18 14:36:55 +03:00
Пью горячий шоклад!
2022-03-01 10:30:23 +03:00
*/
}