Value Object
An immutable domain object defined entirely by its attributes rather than by a unique identity, where two instances with the same values are considered equal.
Also known as: VO, Value Type
Category: Software Development
Tags: domain-driven-design, software-design, patterns, modeling, software-engineering
Explanation
A Value Object is a fundamental building block in Domain-Driven Design (DDD) that represents a descriptive aspect of the domain with no conceptual identity. Unlike entities, which are defined by their unique identity (an Order with ID #12345), value objects are defined entirely by their attributes—two Money objects with the same amount and currency are interchangeable.
**Key characteristics:**
- **Immutability**: Once created, a value object cannot be changed. Operations return new instances rather than modifying existing ones
- **Equality by value**: Two value objects are equal if all their attributes are equal, regardless of reference identity
- **Self-validating**: A value object validates its attributes at construction time—it cannot exist in an invalid state
- **Side-effect free**: Methods on value objects compute and return results without modifying state
- **Replaceable**: Instead of changing a value object, you replace it with a new one
**Common examples:**
- **Money**: Amount + Currency (not just a raw number)
- **Address**: Street + City + PostalCode + Country
- **DateRange**: StartDate + EndDate (with validation that start <= end)
- **EmailAddress**: A string wrapper that enforces email format
- **Color**: RGB values
- **Coordinates**: Latitude + Longitude
**Why value objects matter:**
- **Expressiveness**: `Money(100, 'USD')` is far more meaningful than a raw `float` or `int`
- **Type safety**: You can't accidentally add Money to a Temperature or pass a street address where a ZIP code is expected
- **Encapsulated validation**: Business rules about valid values live in one place
- **Reduced primitive obsession**: Replace scattered primitive types with rich, meaningful domain types
- **Simplified testing**: Immutability and value equality make value objects easy to test and reason about
**Value Object vs. Entity:**
The key question is: does identity matter? If two objects with identical attributes are interchangeable, it's a value object. If you need to track a specific instance across time (this particular customer, that specific order), it's an entity.
Related Concepts
← Back to all concepts