How Scala Embarrasses the Decorator Pattern

The Decorator Pattern is one of the most popular design patterns. It is used in places where everyday programmers probably don’t think much about it, like the way streams in Java can wrap each other adding or modifying functionality as needed.

Scala traits are often thought of as just a kind of hybrid of Java interfaces and abstract classes. But actually Scala’s traits and specifically their “stackability” totally kills the decorator pattern. I’ve covered stackable traits before on the blog. Bill Venners covers stackable traits in more detail.

Once you play around with stackable traits, I don’t think you’ll ever think of doing something like the above UML diagram in Scala code. Nevertheless, it’s the only option in other OO languages. In short, good bye decorator pattern. Scala has you beat.

Leave a Reply

Your email address will not be published. Required fields are marked *