patterns/abstract_factory.dart

56 lines
896 B
Dart

abstract class Drinkable {
void pour();
}
class Beer implements Drinkable {
void pour() {
print("Pouring a delicious beer!");
}
}
class Coffee implements Drinkable {
void pour() {
print("Pouring a refreshing coffee!");
}
}
abstract class DrinkFactory {
Drinkable createDrink();
DrinkFactory();
}
class Pub implements DrinkFactory {
Drinkable createDrink() {
return Beer();
}
}
class CoffeeShop implements DrinkFactory {
Drinkable createDrink() {
return Coffee();
}
}
void main() {
var mood = "sleepy";
late DrinkFactory destination;
switch (mood) {
case "sleepy":
destination = CoffeeShop();
break;
case "done":
destination = Pub();
break;
default:
print("I only have two moods...");
}
var myBeverage = destination.createDrink();
myBeverage.pour();
/*
Pouring a refreshing coffee!
*/
}