O'Reilly logo

Android Development with Kotlin by Igor Wojda, Marcin Moskala

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Decorator pattern

Another common case where the Kotlin class delegation is really useful is when we are implementing a Decorator pattern. A Decorator pattern (also known as Wrapper pattern) is a design pattern that makes it possible to add a behavior to an existing class without using inheritance. In contrast to extensions where we can add a new behavior without modifying an object, we are creating a concrete object with a different behavior. A Decorator pattern uses Delegation, but in a very specific way--delegate is provided from outside of the class. The classic structure is presented in the following UML diagram:

UML diagram of classic ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required