DEV Community


Kotlin Delegated Property – Observable Property

loizenai profile image loizenai ・1 min read

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:

Kotlin Delegated Property – Observable Property

Discussion (0)

Editor guide