👨🏼‍💻 Delegates.Observable Pattern in Kotlin

Mustafa Dincay
Huawei Developers
Published in
4 min readSep 1, 2022

--

Kotlin

Introduction

Hello everyone, in this article I will try to explain the Delegates.observable and Delegates.vetoable patterns in Kotlin. If you’re ready, let’s begin.

The basic logic behind structs such as LiveData and Flow patterns in Android is these observable patterns. Observable is an external pattern. In Kotlin, there are 2 structures, Delegates.observable and Delegates.vetoable. These structures are auxiliary structures so that we can easily use the observable pattern with Kotlin. So what are these patterns used for? The main purpose of the use is if you want to be notified when a new value is assigned to a variable, you can use this pattern. For example, you are working on a banking application and you make a request every 10 minutes to pull the user’s balance value from the backend. You do this setting in the UI according to the incoming value. Instead of checking this every 10 minutes, it would be much more logical to have this action done when this value changes, thanks to an observable pattern. Generally, Livedata and Flow are used in such cases, but since the observable structure is a smaller structure with limited features, you may not want to use Livedata, and Flow in a simple operation. That’s when you can use the observable pattern.

The difference between Delegates.vetoable from Delegates.observable is that the return type of vetoable is Boolean while the return type of observable is Unit. In other words, to give an example, if it is desired to notify when the last value of money rises in a stock market application, and if it is desired to change its value, the vetoable structure should be used. If observable is used, it will be notified in any case whether that value increases or decreases.

In the example below, you see a class named User. It has two properties, name and balance. name is used as Delegates.observable balance is used as Delegates.vetoable. The initial value is given as the initial value in the name property. When a new value is assigned to the name property, the initial value will be the old value and the new value will be the new value. Since the Delegates.vetoable structure is used in the balance property, if the new value is greater than the old value, the value will be changed. Initially, the initial value is given as 0. Then a value of 10 is assigned. As this value, that is, the new value is greater than 0, which is the old value, the assignment is made. However, since the value of 8, which is assigned later, is less than the value of 10, the assignment is not performed.

Kotlin Observable Pattern

The structure is generally like this. But in Kotlin, we can actually use Delegates.observable and Delegates.vetoable structures so that we don’t write boilerplate code. We can write these structures ourselves with the interface or higher-order functions.

As seen in the example above, there is a class called TestObservableInterface. It takes Notify Interface as a parameter. The interface has a function called onNotify. It takes 2 properties of int type as parameters, oldValue and newValue. As you can see, the initial value of balance is given as 0 (ie initial value). Then, the onNotify function is called in the set function, and the field and value are given as parameters. Here, the field, that is, the value that the property holds, corresponds to oldValue, and the value corresponds to the newValue that is assigned later. In the field = value operation, the oldValue becomes newValue and thus the assignment is done. The same structure is available in the TestObservableHigherOrder class. In the parameter part, we see a higherorder function that takes 2 values ​​of Int type and returns Unit back. The same procedures are done here.

Kotlin Observable Pattern with Interface and HigherOrder Function

Conclusion

To summarize, if you need an observable structure in a place so small that you do not want to use LiveData or Flow, you can use Delegates.Observable and Delegates.Vetoable structures in Kotlin according to your needs.

References

Delegated Properties

--

--