Skip to content

Instantly share code, notes, and snippets.

@chrsp
Last active January 25, 2021 17:38
Show Gist options
  • Save chrsp/533d9e5fd9dd61d196ee0cb2fb1cb440 to your computer and use it in GitHub Desktop.
Save chrsp/533d9e5fd9dd61d196ee0cb2fb1cb440 to your computer and use it in GitHub Desktop.
// Module: Domain
struct KitchenUseCase {
// Obtains stuff already stored in some place inside the restaurant
// For example: fetching it from CoreData, KeychainManager, UserDefaults and so on.
private let localDataSource: KitchenLocalRepository
// Obtains stuff needed from outside of our restaurant.
// For example: doing API calls to a NetworkManager.
private let remoteDataSource: KitchenRemoteRepository
// Our entity that knows how to cook a dish. It's a protocol, so each cooker can have
// his own way to prepare the food. However, they must
// always follow our specifications defined in here to deliver the dish
private let cooker: CookerProtocol
public init(localDataSource: KitchenLocalDataSource, remoteDataSource: KitchenRemoteDataSource, cooker: CookerProtocol) {
self.localDataSource = localDataSource
self.remoteDataSource = remoteDataSource
self.cooker = cooker
}
// Defines what our Kitchen should do to cook every dish we have in menu, but don't specify HOW
// the Kitchen will do that. That task will be delegate to ours Cookers
public func prepareRecipe(_ recipe: Recipe) {
switch recipe {
case recipe is SpaghettiCarbonara:
cookSpaghettiCarbonara()
default:
break
}
}
// The set of instructions we provide to our kitchen so can delivery our Carbonara dish.
private func cookSpaghettiCarbonara() {
func cook(using ingredients: [Ingredient]) {
let carbonara = SpaghettiCarbonara(ingredients: storedCarbonaraIngredients)
cooker.cook(carbonara)
}
if let storedCarbonaraIngredients = localDataSource.getIngredients(for: SpaghettiCarbonara.self) {
cook(using: storedCarbonaraIngredients)
} else {
remoteDataSource.getIngredients(for: SpaghettiCarbonara.self) { carbonaraIngredients in
cook(using: carbonaraIngredients)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment