Created
February 29, 2020 01:00
-
-
Save ForceTower/a9aea37898decc6097cfd63971737749 to your computer and use it in GitHub Desktop.
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
fun main() { | |
val field = GameField("Brazil", 80) | |
val grass = GrassGameFieldDecorator(field) | |
val wood = WoodGameFieldDecorator(grass) | |
println("Maintenance started on ${wood.location} that has ${wood.size} km²") | |
wood.maintain() | |
} | |
open class GameField ( | |
val location: String, | |
// km-squared size of the field... because yes... | |
val size: Int | |
) { | |
open fun maintain() { | |
println("Doing nothing more... It is perfect") | |
} | |
} | |
abstract class GameFieldDecorator ( | |
field: GameField | |
) : GameField(field.location, field.size) | |
class GrassGameFieldDecorator ( | |
private val field: GameField | |
) : GameFieldDecorator(field) { | |
override fun maintain() { | |
println("Checking grass for holes...") | |
field.maintain() | |
} | |
} | |
class WoodGameFieldDecorator ( | |
private val field: GameField | |
) : GameFieldDecorator(field) { | |
override fun maintain() { | |
println("Checking field for missing woods...") | |
field.maintain() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can test this here: https://pl.kotl.in/Ofmr2abOC
Or... You can copy + paste this code on Kotlin Playground yourself