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 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.