samedi 8 octobre 2022

Are immutable collections Value Objects?

According to Martin Fowler and other smart folks, Value Objects are immutable objects that are equal due to the value of their properties, as opposed to their identity. Common examples are small "fungible" objects like java.lang.Integer, or compound objects whose overall equality is defined by the equality of the components.

I am curious if immutable collections (like lists, sets, or maps) of immutable objects can be generally considered Value Objects as well. For example, if two separate immutable list objects contain the same objects in the same order, most implementations would consider the two lists equal despite being different objects (e.g., com.google.common.collect.ImmutableList, org.organicdesign.fp.collections.PersistentHashSet). This seems to hold true independent of whether the collection itself contains Value Objects or just regular (non-Value) immutable objects. Even if the collection contains elements that are clearly not Value Objects (e.g., closures or lambdas), two lists would still be considered equal, despite the fact that their components are compared by identity.

Is my above reasoning correct or are there certain immutable collections (of immutable objects) that could not be considered Value Objects? To be clear, I am asking this question from a pure OOP, DDD and Design Patterns perspective. I am aware that "Value Object" has other implications, for example in an ORM context, where it might affect how things are stored in a database. Also, my question assumes a purely functional background where all objects are immutable.

Aucun commentaire:

Enregistrer un commentaire