Simplifying Complex Systems with Value Objects
Values are immutable to prevent the problem in which a different process changes the value’s internal state by mutating it.
Every programming language provides a mechanism to build values. Value objects are immutable. Through the use of composition, we can compute more meaningful values. This helps with the reduction of duplication.
It’s usually a good idea to replace standard primitives, such as strings, with appropriate value objects.
Primitive types are values that must expose their meaning in the context of a sub-domain.
Small objects like points, money, or ranges are suitable value objects. Values can measure things and must be comparable.
Small objects encapsulate specific behaviors and break down complex tasks into manageable pieces. Value objects join attributes to remove duplication and enable rich behavior.
Value objects act as foundational pieces for a system.
[¹]: Domain-Driven Design: Tackling Complexity in the Heart of Software