October 2018
Intermediate to advanced
370 pages
9h 15m
English
The decorator pattern allows us to add behavior to the individual object dynamically, without affecting the behavior of other objects from the same class. We can achieve this by wrapping the object into another instance. The following diagram shows a case like this:

The diagram contains the AmateurCoffeeMachine and ProfessionalCoffeeMachine classes, which implement the CoffeeMachine interface.
The following example shows how this pattern works:
interface CoffeeMachine { val leftCoffeeMilliliters: Int val leftMilkMilliliters: Int fun makeCoffee(milliliters: Int): Coffee { leftCoffeeMilliliters - milliliters return Coffee(milliliters) ...
Read now
Unlock full access