DEV Community

loizenai
loizenai

Posted on

Kotlin Delegated Property – Observable Property

https://grokonez.com/kotlin/kotlin-delegated-property-observable-property

Kotlin Delegated Property – Observable Property

In this Kotlin tutorial, we're gonna look at how to make an observable property by using Kotlin delegated property.

I. Technology

  • Java 1.8
  • Kotlin 1.1.2

    II. Overview

    We will create an example that can notify listener when a property of an object changes.

Java has a mechanism for this notification: PropertyChangeSupport class manages a list of listeners and dispatches PropertyChangeEvent events to them.

To use it, we will create a helper class that will store a PropertyChangeSupport instance and keep track of PropertyChangeListener.


open class PropertyChangeAware {

    protected val propertyChangeSupport = PropertyChangeSupport(this)
    fun add/remove PropertyChangeListener(listener)
}

Then the class that has property to be notified will extend this helper class:

https://grokonez.com/kotlin/kotlin-delegated-property-observable-property

Kotlin Delegated Property – Observable Property

Top comments (0)