Value Object

Definition
An immutable object whose equality is not based on identity
Value object

How does it work?

Value objects are simple or compound values like Money, Date, or PolicyNumber. They are identified by the combination of their attributes, and don't have an id attribute. Their main purpose is to group some attributes, and to validate their values. Since the attributes are validated on creation, a value object is always valid, and that means that it never has to be checked again.

The object is immutable, it's attributes don't change value after they have been created. Their equals function compares all attributes of both objects.

Next to its attributes, it may have some utility methods.

Examples

  • Money (amount and currency)
  • Date (fixed format; i.e. yyyy-mm-dd)
  • DateTime (date and time)
  • Address

When should you use it?

  • When you find yourself using string for dates, for example, and you wonder if there's a way to enforce that the format of the date is fixed.

Links