This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Usage | |
let happyMeal1: [any RestaurantItem] = | |
[PizzaType.margherita, SideDishType.fries, DrinkType.lemonade] | |
let happyMeal2: [any RestaurantItem] = | |
[PizzaType.farmhouse, SideDishType.calzone, DrinkType.soda] | |
let pizzaShop = PizzaShop() | |
pizzaShop.mealOrder(order: happyMeal1) | |
pizzaShop.mealOrder(order: happyMeal2) | |
pizzaShop.mealOrder(order: [SideDishType.calzone, DrinkType.lemonade]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PizzaShop { | |
var orderNumber: Int = 1 | |
func onePlusOneOffer(order: some RestaurantItem) { | |
print("Order #\(orderNumber) starting") | |
for _ in 0 ..< 2 { | |
print(order.acceptOrderItem()) | |
print(order.prepareOrderItem()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum DrinkType: String, RestaurantItem { | |
case soda = "Soda" | |
case lemonade = "Lemonade" | |
func acceptOrderItem() -> String { | |
"\(self.rawValue) drink in preparation" | |
} | |
func prepareOrderItem() -> String { | |
"\(self.rawValue) drink is ready!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum SideDishType: String, RestaurantItem { | |
case fries = "Fries" | |
case calzone = "Calzone" | |
func acceptOrderItem() -> String { | |
"\(self.rawValue) side dish in preparation" | |
} | |
func prepareOrderItem() -> String { | |
"\(self.rawValue) side dish is ready!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum PizzaType: String, RestaurantItem { | |
case margherita = "Margherita" | |
case farmhouse = "Farmhouse" | |
case goldenCorn = "Golden corn" | |
var toppings: [PizzaTopping] { | |
switch self { | |
case .margherita: | |
return [.lotsOfCheese] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol RestaurantItem { | |
associatedtype Message = String | |
func acceptOrderItem() -> Message | |
func prepareOrderItem() -> Message | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum PizzaTopping: String { | |
case onion = "Onion" | |
case capsicum = "Capsicum" | |
case corn = "Corn" | |
case cheese = "Cheese" | |
case lotsOfCheese = "Lots of cheese" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Usage | |
//Pizza orders | |
var myOrder = PizzaShop() | |
myOrder.prepare(type: PizzaType.margherita) | |
var myOrder2 = PizzaShop() | |
myOrder2.prepare(type: PizzaType.farmhouse) | |
var myOrder3 = PizzaShop() | |
myOrder3.prepare(type: PizzaType.goldenCorn) | |
//Side dish orders | |
var myOrder4 = PizzaShop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PizzaShop { | |
func prepare<T: ItemName>(type: T) { | |
print(type.message) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum SideDishType: String, ItemName { | |
case fries = "Fries" | |
case calzone = "Calzone" | |
var message: String { | |
"\(self.rawValue) side dish is ready!" | |
} | |
} |
NewerOlder