October 2019
Intermediate to advanced
434 pages
11h 54m
English
In our previous example of Chess, we defined concrete strategies by implementing the BotBehavior interface. This interface was quite simple and defined only a single method. In situations like these, we could simplify our Strategy pattern implementation by relying on higher-order functions instead of concrete types. Let's see how this is done:
class FunctionalChess(val behavior: (Array<Array<Int>>) -> Unit) { fun play() { // game loop // relies on behavior } fun pause() { }}
This new FunctionalChess class takes a function parameter instead of an instance of BotBehavior. This will enable us to define our behavior strategies as functions and ...
Read now
Unlock full access