Delegation is defined as the granting of any authority or power to another person (Boss assigning tasks to its employees) to carry out different work. However, the person who delegated the work remains accountable for the outcome of the delegated work. In a similar manner, there are various types of properties in a programming language that we can implement manually every time we want to use them to delegate other code work and we can implement them once and for all and put into the library if continuously used in our program’s.
There are mainly 3 types of properties used for delegation:
- Lazy Properties: Lazy is a lambda function that takes in property to implement input and return an instance of Lazy where T denotes the type of property being used to implement lazy property. By default, lazy is synchronized means the value is computed at first call and the same value will be returned on other calls as well, we can stop the synchronization using LazyThreadSafetyMode.PUBLICATION as a parameter to the lazy() function.
// Defining an inmutable variable in kotlin
val lazyValue: String by lazy
// Printing to standard output
// Printing Var lazyValue values to standardoutput
GeeksforGeeks GeeksforGeeks GeeksforGeeks
Here, the value is computed only in “println”, and println calls lazyValue and the vale in println is printed as output and all the callbacks will see the same the value due to synchronization.
- Observable Properties: It notifies the user about the changes made into the class using handler which contains information about changes taking place. It is used to get notified via callbacks whenever the property changes. It contains two arguments to initialize the object with, new property or value(the initial value) and handler which contains information about all the modifications we will make by assigning the input to the object (it contains a property to be assigned, old value, the new value assigned).
// Creating a Myself Class with String variable name
var name: String by Delegates.observable(
// default Previous value "company name"
"$old -> $new"
// Old value is being
// assigned a new value
// Assigning name variable defalut value
val V1 = Myself()
// Assigning name variable a new value "Previous to"
// Now again changing "Previous to " to "New Value" value
company name -> Previous to Previous to -> New Value
Here the observable properties take in two arguments, the initial value as “company name” and second the handler which we call every time we assign it to the property with three values as “property to be modified, the previous value and new value.
- Vetoable: It allows us to modify the values when the argument input by the user(s) fulfills the specified condition, it can be used in place of observable properties if the user wants to intercept assignment. Vetoable is like Observable Properties with additional features to allows to modify and notify the values when the condition is met.
0 10 10
Firstly the default value is printed and afterward, the new max value is assigned and the value was not changed when the above callback was invoked again due to failure to satisfy the property. When the callback returns true it means the value of the property is changed to a new value, else the new value is discarded and the property still contains its old(previous) value.
This works also for var’s properties if you use a Mutable Map instead of read-only Map
Here we created a Myself class, containing a map function, for accepting the input in form of string or any(int, float, string, etc) and storing them in my_name and my_age variables in map function inside myself class and we can use the map function dynamically inside any newly created class as well.
- Kotlin | Class Properties and Custom Accessors
- HTML | DOM Window opener Properties
- HTML | DOM Window frames Properties
- Transition shorthand with multiple properties in CSS?
- HTML | DOM Window frameElement Properties
- HTML | DOM Window localStorage Properties
- HTML | DOM Window defaultStatus Properties
- C# | Restrictions on Properties
- How to override the CSS properties of a class using another CSS class ?
- How to set default values for Angular 2 component properties?
- Kotlin Data Types
- Hello World program in Kotlin
- Kotlin | Retrieve Collection Parts
- Destructuring Declarations in Kotlin
- Why Kotlin will replace Java for Android App Development
- DatePicker in Kotlin
- Kotlin labeled continue
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.