August 2017
Intermediate to advanced
440 pages
10h
English
In object-oriented programming, the Delegation pattern is a design pattern that 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 which holds all the 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() ...Read now
Unlock full access