Explicit delegation

Explicit delegation can be implemented by passing the delegate object to the delegating object. We can do this in any object-oriented language, such as Java. Let's create a Calculator interface:

public interface Calculator {    int performOperation(String operand);}

We'll also create a CalculatorBrain class that implements the Calculator interface:

public class CalculatorBrain implements Calculator {    public int performOperation(String operand) {        throw new IllegalStateException("Not implemented!");    }}

Now we'll implement a CalculatorMachine class. An instance of this class is a delegating object that delegates authority to an instance of Calculator, which is a delegate object. Let's create the CalculatorMachine class, which ...

Get Mastering High Performance with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.