Class Delegation


Class Delegation

Delegation governs the transfer of authority /authority from one context to another for any object. Delegations can be used in permanent and interchangeable relationships for delegating classes and functions. Inheritance implementation of classes and functions can be modified with the help of delegation techniques and object-oriented programming languages, which are supported by default without boilerplate code. Delegation is used in Kotlin with the help of the "by" keyword.

There are two types of delegations:

  1. Explicit Delegation - All object-oriented languages are supported, which is done by transferring a delegate (one to be executed) object to the delegate object (one that performed the assigned object).
  2. Implicit Delegation – The delegation pattern requires language-level support.

Using Lazy () Function - Lazy is a lambda function that takes property as input and instead gives an example of lacy where is the type of properties it uses.

Using Delegation.Observable () Function - Observable() function takes two arguments to initialize the object and returns to the called function. Syntax is the expression after the keyword "by" is delegated. The get () and set () methods of the variable will be delegated to its getValue () and setValue () methods as defined in the delegate class.