Skip to content

Instantly share code, notes, and snippets.

@miquelbeltran
Created September 22, 2019 15:21
Show Gist options
  • Select an option

  • Save miquelbeltran/e86fc84ea5142ff5bcc2a4003f2b01c1 to your computer and use it in GitHub Desktop.

Select an option

Save miquelbeltran/e86fc84ea5142ff5bcc2a4003f2b01c1 to your computer and use it in GitHub Desktop.
abstract class Beverage {
open val description: String = "Unknown Beverage"
abstract val cost: Double
}
abstract class CondimentDecorator : Beverage() {
abstract override val description: String
}
class Espresso: Beverage() {
override val description = "Espresso"
override val cost: Double = 1.99
}
class HouseBlend: Beverage() {
override val description = "House Blend"
override val cost: Double = .89
}
class Mocha(beverage: Beverage): CondimentDecorator() {
override val cost: Double = beverage.cost + .20
override val description: String = beverage.description + ", Mocha"
}
class Milk(beverage: Beverage): CondimentDecorator() {
override val cost: Double = beverage.cost + .30
override val description: String = beverage.description + ", Milk"
}
fun main() {
val espresso = Espresso()
println(espresso.description + " " + espresso.cost)
val espressoWithMilk: Beverage = Milk(Milk(beverage = HouseBlend()))
println(espressoWithMilk.description + " " + espressoWithMilk.cost)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment