Skip to content

Instantly share code, notes, and snippets.

@dodalovic
Last active April 6, 2021 13:08
Show Gist options
  • Save dodalovic/a22baaadc890a7aba33ff5347282198c to your computer and use it in GitHub Desktop.
Save dodalovic/a22baaadc890a7aba33ff5347282198c to your computer and use it in GitHub Desktop.
Decorator pattern in Kotlin
package patterns
interface CarService {
fun doService()
}
interface CarServiceDecorator : CarService
class BasicCarService : CarService {
override fun doService() = println("Doing basic checkup ... DONE")
}
class CarWash(private val carService: CarService) : CarServiceDecorator {
override fun doService() {
carService.doService()
println("Washing car ... DONE")
}
}
class InsideCarCleanup(private val carService: CarService) : CarServiceDecorator {
override fun doService() {
carService.doService()
println("Cleaning car inside ... DONE")
}
}
fun main(args: Array<String>) {
val carService = InsideCarCleanup(CarWash(BasicCarService()))
carService.doService()
}
Doing basic checkup ... DONE
Washing car ... DONE
Cleaning car inside ... DONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment