The value object design pattern
In programming, there are different ways of comparing data. We can compare object identities or their values. These are useful in different scenarios and here we will see what value objects are and when they can be used. Value objects are:
Note
Small and simple immutable objects. Their equality is based not on identity, but on value equality.
Value objects are used to represent numbers, money, dates, and so on. They should be small and immutable; otherwise, changing values could cause bugs and unexpected behavior. They are quite useful in multithreaded applications due to their immutability. They are also commonly used as data transfer objects in enterprise applications.
Class diagram
In languages such as Java, there is no direct support for value objects. What developers end up doing is 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...