June 2018
Intermediate to advanced
316 pages
6h 34m
English
The observer pattern is a commonly-used pattern in any programming language. The Kotlin standard library contains a special delegate that implements this pattern:
var subject: String by Delegates.observable("init value") { property, old, new -> println("$old -> $new")}
In this code snippet, we can see a subject variable that has an observer. The observer is a lambda that's passed as the second parameter to the observable function. The observable function looks like this:
public inline fun <T> observable(initialValue: T, crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Unit): ReadWriteProperty<Any?, T> = object : ObservableProperty<T>(initialValue) { override fun afterChange(property: KProperty<*>, ...
Read now
Unlock full access