The decorator design pattern the Scala way

As with the other design patterns, this one has an implementation that takes advantage of the richness of Scala and uses some of the concepts we looked at throughout the initial chapters of this book. The decorator design pattern in Scala is also called stackable traits. Let's see what it looks like and how to use it. The InputReader and AdvancedInputReader code will remain exactly as shown in the previous section. We are actually reusing it in both examples.

Next, instead of defining an abstract decorator class, we will just define the different reader modifications in new traits as follows:

trait CapitalizedInputReaderTrait extends InputReader {  abstract override def readLines(): Stream[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.