Self types for components

As an example, let's imagine that we are trying to build a robot that cooks food. Our robot will be able to look up recipes and cook the dishes we ask for, as well as tell us the time. We will be able to add extra functionality to our robot by simply creating new components.

We want our code to be modular, so it makes sense to split the functionality. The following diagram shows what our robot will look like and the relationships between the different components:

First of all, let's define the interfaces for the different components:

trait Time {  def getTime(): String}trait RecipeFinder { def findRecipe(dish: String): ...

Get Scala Design Patterns - Second Edition 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.