The stackable traits design pattern
There are sometimes cases where we want to be able to provide different implementations for a method of a class. We might not even know all the possibilities that could exist at the moment of writing, but we can add them later and combine them together. Or we can allow someone else to do this instead. This is another use case of the decorator design pattern, which for this purpose could be implemented with the stackable traits design pattern. We have already seen this pattern before in this book in Chapter 7, Structural Design Patterns, but we used it to read data, which adds a really important catch there. We will see another example here, which will make sure everything is completely clear.
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...