Skip to content

Instantly share code, notes, and snippets.

@ForceTower
Created February 29, 2020 01:00
Show Gist options
  • Save ForceTower/a9aea37898decc6097cfd63971737749 to your computer and use it in GitHub Desktop.
Save ForceTower/a9aea37898decc6097cfd63971737749 to your computer and use it in GitHub Desktop.
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()
}
}
@ForceTower
Copy link
Author

You can test this here: https://pl.kotl.in/Ofmr2abOC
Or... You can copy + paste this code on Kotlin Playground yourself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment