Kotlin Delegating Properties Tutorial with Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will learn about delegating properties in kotlin in last video we have already discussed what as delegation pattern and how kotlin supports it natively delegation pattern basically means delegating the responsibility to other object and we can apply this concept to properties as well but before moving into property delegation let's try to understand why it is needed so for the example i will create a class here let's say we have a class student like this and inside this class we have two vars so inside the class student we have two wires for first name and last name and we want to add some validations when we will set the value for first name and last name and we can do this thing using setter so i will create a setter for first name so let's say we want f value is not equals to null and the length of the value is greater than 5. and we will also decorate the value and what we will do is we will assign the field equals to value dot trim we will trim the spaces and we will convert the value to upper case so this is the validation that we are doing and the same thing we will do for last name as well now the problem with this approach is we are repeating the same kind of code you can see the same thing we have written in two places so what we can do is we can delegate this responsibility to other object and it is when the delegation pattern is helpful now this is very small example and you might think that it is not necessary but think about it when you have more than two variables and you have the same code in multiple places then delegating it to an other object and keeping this code at one place is a very good idea because later if you want to change the condition you need to change it in every place manually but if you will delegate it to an other object that has only one place you will change only one place and that is why it becomes handy and you will have less chances of making an error so let's see how we can delegate this responsibility to other object and to do this we will create one more class so i will create another class here only and let's say we have a class and it is named name delegate you can name it whatever you want so we have a class named name delegate and here i will define a where it is formatted value of type string and the initial value is null now we need to create getter and setter operators so for the setter we write operator 1 and then set value like this and inside this function the first parameter that we will have is the calling object so we can write it as this ref you can write any name but the type will be any the next parameter will be property and it is of type k property like this and the last parameter is the value itself like this now we have a function set value that will set the value to our formatted value so now we will copy this thing and we will paste it here and i forget to make the value nullable so everything is fine just in place of field we need to write formatted value so whenever we will do a set operation or we will assign a value to our first name that will be delegated by this name delegate this function will be called and we will do this logic while assigning the value now we can also create an operator fun get value and this will return the formatted value now for the parameters we need this ref and the property like this and here we need to write the return statement so this get value will give us the string that is formatted now this ref means this is the calling object and that is why the type as any so that we can cast it to any object for example we may have other properties of this class student that we want to access here then what we can do is we can write any as student not any actually we need to write this ref so this ref is the object and is the type so we will cast this ref as student because as is the keyword in kotlin that we use to type cast and then we can access the members of student class so this ref is for the calling object property contains the meta info and value is the value that we are setting and in this case this ref is the calling object property contains the amanda info and we are returning the formatted value that we have here so that's it for the delegate now what we can do is instead of using setters we can use delegates so what we will do is we will write first name of type nullable string and we will use the by keyword by name delegate like this and the same thing we can do with last name so now we do not have repeating code in our file and it is a better way to do these kind of things so now let's try running the code to see if it is working or not so what i will do is i will create an instance of student so we have student now let's assign first name let's say i will write my name now we can print the student so let's print student now it will not print the name because it is a class so what we can do is we can override the tostring function so whenever we want to print the class we want first name and last name like this now it is fine let's run the code you can see we are getting bilal khan what if i will assign just empty spaces it will be null because the length is less than five you can see we are getting null null if you will try to assign just two characters you will get null so our logic is working absolutely fine so that is all for this video friends this is how we delegate the properties in kotlin and it supports it with the by keyword so in case you have any question or confusion you can leave your comments below and i will see you in the next video thanks for watching you
Info
Channel: Simplified Coding
Views: 3,722
Rating: 4.9428573 out of 5
Keywords: kotlin delegated properties, kotlin delegate, kotlin tutorial, kotlin oop, kotlin tutorial for beginners, kotlin android tutorial, android kotlin tutorial, learn kotlin, kotlin programming tutorial, kotlin programming course, android development
Id: rWPqjr1ZmQU
Channel Id: undefined
Length: 8min 33sec (513 seconds)
Published: Wed Jan 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.