Code example

It is time to see how the flyweight design pattern looks like when expressed in Scala code. We will be using the same example as shown previously. It is worth noting that in the code version, some classes have different names than in the diagram. The reasons for this are Scala naming conventions. We will explicitly point out where this happens while going through the code.

An interesting thing about the flyweight design pattern and our example is that it actually uses other design patterns and techniques that we already went through before. We will also point them out while looking at the code.

The first thing we've done is represent the colors. This has nothing to do with the actual flyweight design pattern, but we've decided ...

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.