Skip to content

Instantly share code, notes, and snippets.

@MamboBryan
Created June 21, 2023 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MamboBryan/1ef885ce9ab4c411f07179960a47ca73 to your computer and use it in GitHub Desktop.
Save MamboBryan/1ef885ce9ab4c411f07179960a47ca73 to your computer and use it in GitHub Desktop.
package com.bizyback.play.circletest
/**
* The implementation of the prepare function is abstracted from the callers
*/
interface Food {
fun prepare(time: Int): String = "Preparing ${getIngredients()} as a ${this::class.simpleName} in $time minutes"
fun getIngredients(): String
}
data class Meal(val name: String = "biryani") : Food {
override fun getIngredients() = name
}
data class Salad(val ingredients: List<String> = listOf("tomato", "onions")) : Food {
override fun getIngredients() = ingredients.joinToString()
}
fun main() {
val salad: Salad = Salad()
val meal: Meal = Meal()
cook(salad)
cook(meal)
cook(salad as Food)
}
/**
* THE cook function is polymorphic and has different forms
*/
fun cook(meal: Meal) {
println(meal.prepare(10))
}
fun cook(salad: Salad) {
println(salad.prepare(15))
}
fun cook(food: Food) {
println(food.prepare(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment