An example class diagram

In languages such as Java, there is no direct support for value objects. What developers end up doing is to declare the fields as final and implement the hashCode and equals methods.

Immutability, however, is a concept that is pretty much enforced in Scala. We already saw the algebraic data types (ADTs) earlier—they also fall in the value object category. Case classes and tuples are also immutable and they are used to achieve the value object design pattern. The following class diagram shows an example of the value object design pattern in Scala:

This diagram really doesn't do anything special. It is a representation ...

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.