O'Reilly logo

Android Development with Kotlin by Igor Wojda, Marcin Moskala

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Delegation pattern

In object-oriented programming, Delegation pattern is a design pattern, which is an alternative to inheritance. Delegation means that the object handles a request by delegating it to another object (delegate), instead of extending the class.

To support the polymorphic behavior known from Java, both objects should implement the same interface that holds all delegated methods and properties. A simple example of the delegation pattern is the following:

 interface Player { // 1 fun playGame() } class RpgGamePlayer(val enemy: String) : Player { override fun playGame() { println("Killing $enemy") } } class WitcherPlayer(enemy: String) : Player { val player = RpgGamePlayer(enemy) // 2 override fun playGame() { player.playGame() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required