Introduction to delegation

The origin of delegation in programming is from object composition. Object composition is a way to combine simple objects to derive a complex one. Object compositions are a critical building block of many basic data structures, including the tagged union, the linked list, and the binary tree.

To make object composition more reusable (as reusable as inheritance), a new pattern is incorporated—the delegation pattern.

This pattern allows an object to have a helper object, and that helper object is called a delegate. This pattern allows the original object to handle requests by delegating to the delegate helper object.

Though delegation is an object-oriented design pattern, not all languages have implicit support for delegation (such as Java, which doesn't support delegation implicitly). In those cases, you can still use delegation by explicitly passing the original object to the delegate to a method, as an argument/parameter.

But with the language support (such as in Kotlin), delegation becomes easier and often seems like using the original variable itself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.177.86