Kotlin Standard Delegates — Lazy, Observable and Vetoable
Delegation is an object-oriented design pattern and Kotlin supports it’s natively. If you are not familiar with the native support of delegation by Kotlin read this.
Delegate your Kotlin Code and Delegating Properties
The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it…
Kotlin standard library has some predefined set of delegates that can be used.
Suppose you have a property that is computationally expensive to instantiate and might not be ever needed or just for a single instance is needed. In this scenario lazy delegate is beneficial. The lazy delegate allows the value of a property to be computed only on first access and then cached.
By default, the evaluation of lazy properties is synchronized: the value is computed only in one thread, but all threads will see the same value.
Suppose a scenario where you want to observe every change in the value of the parameter the old value and the new value. The handler is called every time you assign to the property (after the assignment has been performed). The observable delegate allows for a lambda to be triggered any time the value of the property changes.
Suppose a scenario, where you want to intercept the assignment of a variable value. Like you have the condition until and unless it is satisfied the value of the field will not get changed.
As of Kotlin 1.4, it is also possible to delegate directly to another property. For example, if we are renaming a property in an API class, we may leave the old one in place and simply delegate it to the new one. To delegate property to another property, use the
:: the qualifier in the delegate name, for example,
Delegated properties | Kotlin
Lazy properties: the value is computed only on first access. Observable properties: listeners are notified about…
Don’t Reinvent the Wheel, Delegate It!
Favouring delegation to inheritance with Kotlin native support
I will well appreciate one of these 👏
Build Ktor Project with Docker and Add Nginx for Load Balancing & Reverse Proxy
We’ll see how to deploy a Ktor application to a Docker container, which can then be run either locally
Looking Into AsyncTask and WeakReference in Android💫
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the…
Looking Back Into HandlerThread Android Internal💫
A Thread that has a Looper. The Looper can then be used to create Handler s. … Note that just like with a regular…
Making a Smartwatch Wear Face with Watch Face Studio⌚
Watch Face Studio is a graphical authoring tool that enables you to create watch faces for the Wear OS smartwatch…