Updated April 6, 2023
Introduction to Kotlin delegate
The kotlin delegate is one of the design patterns that can be used to implement the application concepts like inheritance with the help of keywords like “by” or delegation methodology it’s used to derive the class to public access it implements with the other concepts like interface that allowed to call the specific object delegate used other keywords like public, default also the lazy values gets computed only in the parent classes also created anonymous objects without creating a class using the interfaces, properties and other default standard libraries other delegation types like explicit it supports oops and other delegation objects.
Syntax of Kotlin delegate
In kotlin language, we used many default keywords, variables, and other built-in functions. Like that delegate is one of the concepts and the design pattern which helps to implement the application. With the help of “By” keyword we can achieve the delegation in the kotlin language.
interface first{
---functions declaration—
}
class classname() : first{
---override the function declaration with name which is used by the interface—
}
class name2(variable: interface name(first)) : first by variable
fun main()
{
--some logic codes depends on the requirement—
}
The above code is the basic syntax for utilizing the kotlin delegation in the application.
How does delegate work in Kotlin?
The kotlin language has many design patterns like java and other languages. Each design pattern has implemented its own logic and reduces the code complexity easily track the codes with other new users. Like that delegation is one of the design patterns and it is used to replace or on behalf of the other values the object request is received by one variable and instead of that variable will use another variable with the same logic and output results.
So that it is one of the easiest methods for providing support for both class and other properties that can be delegated to the pre-built in classes and methods. Generally, the kotlin delegation is achieved using the “by” keyword that delegates the kotlin functionality from the other interfaces with other methods. Each method has a separate behavior and its attribute.
In delegates, it is especially used to inherit from the particular class that may be the hierarchy one, and the same will be shared with the interface and decorates both internal and external objects of the original type. This can be achieved using the public APIs that delegate with the properties which are either set and get calls handling by using the object.
Examples of Kotlin delegate
Given below are examples of Kotlin delegates.
Example #1
Code:
interface first
{
fun demo()
fun demo1()
}
class example(val y: String) : first
{
override fun demo()
{
print(y)
}
override fun demo1()
{
println(y)
}
}
class example1(f: first) : first by f
{
override fun demo()
{
print("Welcome To My Domain its the first example that related to the kotlin delegation")
}
}
data class examples(val user: String, val ID: Int, val city: String)
fun main()
{
val b = example("\nHave a Nice Day users, Please try again!")
example1(b).demo()
example1(b).demo1()
val inp1 = listOf(
examples("Siva", 1, "your location is chennai"),
examples("Raman", 2, "your location is tiruppur"),
examples("Siva Raman", 3, "your location is mumbai"),
examples("Arun", 4, "your location is andhra"),
examples("Kumar", 5, "your location is jammu"),
examples("Arun Kumar", 6, "your location is kahmir"),
examples("Madhavan", 7, "your location is madurai"),
examples("Nayar",8, "your location is karnataka"),
examples("Madhavan Nayar", 9, "your location is delhi"),
examples("Rajan", 10, "your location is west bengal"),
)
val inp2 = inp1
.filter { it.user.startsWith("M") }
.maxByOrNull{ it.ID }
println(inp2)
println("Your input user lists are : ${inp2?.user}" )
println("The user IDs are shown: ${inp2?.ID}" )
println("city: ${inp2?.city}" )
println("Thank you users for spenting the time with our application kindly try and spent more with our application its useful for your knowledge, $inp1")
}
Output:
In the first example, we used delegates design pattern with a collection list to perform the datas in array operations.
Example #2
Code:
import kotlin.properties.Delegates
class Employee {
var EmployeeName: String by Delegates.observable("<no EmployeeName>") {
prop, oldName, NewName ->
println("$oldName -> $NewName")
}
}
class EmployeeDetails {
var Id: Int = 0
var oldID: Int by this::Id
}
val eg = fun(a: Int, b: Int): Int = a + b
val eg1 = fun(a: Int, b: Int): Int {
val multipl = a * b
return multipl
}
val eg2 = fun(a: Int, b: Int): Int = a - b
fun demo1(a: Int, b: Int, demo: (Int) -> Unit ){
val addition = a + b
demo(addition)
}
fun demo2(a: Int, b: Int, demo: (Int) -> Unit ){
val subtraction = a - b
demo(subtraction)
}
fun main() {
println("Welcome To My Domain its the second example that related to the kotlin delegates")
println("Thank You users have a nice day")
val Employee = Employee()
Employee.EmployeeName = "first"
Employee.EmployeeName = "second"
val EmployeeDetails = EmployeeDetails()
EmployeeDetails.oldID = 41
println(EmployeeDetails.Id)
val demo1: (Int) -> Unit= {s: Int -> println(s) }
val sum = eg(23,34)
val multipl = eg1(34,23)
val minus = eg2(34,23)
println("Thank you users the sum of two numbers is: $sum")
println("Thank you users the multiply of two numbers is: $multipl")
println("Thank you users the subtraction of two numbers is: $minus")
val new = { println("Thank you for using the kotlin delegates concepts in the application!")}
new()
new.invoke()
val new1 = arrayOf(27,71,93)
new1.forEach { anoresult -> println(anoresult * anoresult) }
new1.forEach { println(it*it) }
}
Output:
In the second example we used the delegates pattern additionally we called some lambda expressions with the arithmetic operations.
Example #3
Code:
class Third {
var var1: Int = 13
var var2: Int by this::var1
}
fun main() {
val Third = Third()
Third.var2 = 42
println("Welcome To My Domain its the third example taht related to the kotlin delegates")
println(Third.var1)
}
Output:
In the final example, we used the delegates pattern with the help of by keyword.
Conclusion
In conclusion, kotlin uses many concepts like interface, classes, anonymous classes here in the delegate pattern without class we can create anonymous objects. The kotlin interface delegation should must know that when will be used and how to configure it on the application logic via code without affecting existing areas.
Recommended Articles
This is a guide to Kotlin delegate. Here we discuss the introduction, syntax, and working of delegate in kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –