Using stackable traits

The stackable traits design pattern is based on mixin composition—something we became familiar with in the early chapters of this book. We usually have an abstract class or a trait that defines an interface, a base implementation, and traits that extend the abstract class to stack modifications on it.

For our example, let's implement the following diagram:

The preceding diagram is of a really simple application. We have a base StringWriter class, which has a basic implementation (BasicStringWriter) that just returns a message containing the string. On the right-hand side, we have traits that can add stackable modifications ...

Get Scala Design Patterns - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.