Modelling and persistence
What are value objects?
On this page
A value object represents something whose attributes determine its meaning. Two Money values with the same currency and minor-unit amount can be interchangeable. Immutability makes sharing safe. Decide on rounding and currency compatibility before exposing arithmetic or equality.
Worked example
Pythonfrom dataclasses import dataclass
@dataclass(frozen=True)
class Money:
currency: str
cents: int
prices = {Money("EUR", 125), Money("EUR", 125)}
assert len(prices) == 1
assert Money("USD", 125) not in pricesExplore class relationships in the OOP Playground