Alternative implementation

The value object design pattern can also be achieved with predefined tuple classes in Scala. In this case, we don't even need to create our class and we can write something like (3, "March", 2016). This would automatically have the same characteristics as value objects. There are implementations of tuples of up to 22 elements, but using them in real applications is not recommended, as readability and quality can degrade dramatically. Moreover, two n element tuples can be considered equal, even if semantically they are different types of objects in our application. Last but not least, accessing elements using case classes is much easier and nicer to read than writing something like tuple._3.

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.