Delegate your Kotlin Code and Delegating Properties

Photo by Matt Hardy on Unsplash

Delegation is an object-oriented design pattern and Kotlin supports it natively. Okay first let me explain to you what is delegation pattern.

Delegation Pattern means delegating the responsibility to other objects. It allows an object to communicate back to its owner in a decoupled way. By not requiring an object to know the concrete type of its owner, we can write code that is much easier to reuse and maintain.

We try to implement a small example to help ourselves. Here we have made two interfaces Downloader and Uploader. We then added the implementation in Mp3Downloader and Mp3Uploader.

Finally, We make FileHandler, which takes two parameters of type Downloader and Uploader, we further implement the functions.

When we are making an instance of FileHandler in the main() we are passing Mp3Downloader and Mp3Uploader instances.

The Class FileHandler in the above example can be simplified by can be reduced to. This will work in the same way and we don’t need to write any boilerplate code.

That’s it, a simple example of Delegation and how you can use it!!

Delegating Properties in Kotlin

Think of a scenario where you have to check some delegation before setting the value to your variable. Here we want to check the condition, then trim, then convert to uppercase. You see that the code here is repetitive delegation comes to the rescue.

We delegate the checking and setting part so that it can be reduced. We have to write a class that extends either ReadOnlyProperty or ReadWriteProperty. Both of these interfaces define a method called getValue() — which is used to supply the current value of the delegated property when it is read. The ReadWriteProperty interface additionally defines a method called setValue()that is used to update the current value of the property when it is written which we will use here. The ReadWriteProperty interface actually extends ReadOnlyProperty.

Firstly, we have to make a class StringDelegate, then we add a variable formatedValue of type String. Here we have passed the value required parameter to setValue() and getValue()

thisRef: A reference to the class that the property is in
property: A reflection description of the property being delegated
value: The new value of the property

thisRef will be typecast to the class to get class parameters, the nameLength that you can see in the below example.

This the final results you can clearly see the difference and the reusability of StringDelegate class.

Property delegation is a powerful technique, that allows you to write code that takes over control of other properties, and helps this logic to be easily shared amongst different classes. This allows for robust, reusable logic that looks and feels like regular property access.

For any doubts and suggestions, you can reach out on my Instagram, or LinkedIn. Follow me for Kotlin content and more. Happy Coding!

I will well appreciate one of these 👏

Recent Posts:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store