Created
September 22, 2019 15:21
-
-
Save miquelbeltran/e86fc84ea5142ff5bcc2a4003f2b01c1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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