13 Nov 2023
-
Money: Consider $10 USD. It is immutable once created; you can't change its value. Its equality is based on the currency and the amount; two instances representing the same amount in the same currency are considered equal, irrespective of where or when they were created.
-
Address: An address comprising street, city, state, and postal code is immutable. Two address objects with the same components (same street, city, state, and postal code) are considered equal, even if they're used in different contexts.
-
Color: Defined by its RGB or HEX values, a color object remains unchanged and equal if it has the same color code (e.g., #FF0000 for red). Two color objects with the same code represent the same color.
-
Date Range: Immutable with a start and end date, its equality is defined by these dates. If two date range objects share the same start and end dates, they represent the same time span and are considered equal.
-
Measurements: Immutable representations of quantities, such as 5 meters. Equality is based on the numerical value and the unit; two measurement objects with the same value and unit are considered equal.
-
Geo Coordinates: Immutable latitude and longitude pairs representing a location. Two coordinate objects with the same latitude and longitude values represent the same position, making them equal.
-
Email Address: Defined by its string value, an email address object is immutable and equal to another email address object with the same email string. If two email address objects have the same email string, they are considered equal, regardless of where they are used.
-
Temperature: An object representing a specific temperature, defined by a numerical value and a unit (e.g., 25 degrees Celsius or 77 degrees Fahrenheit). Equality is based on both the value and the unit.
-
Phone Number: An object representing a phone number with its country code and number. Two phone number objects with the same country code and number are considered equal.
-
Duration: An object representing a specific amount of time, defined by hours, minutes, and seconds. Equality is based on the duration's components.
-
ISBN (International Standard Book Number): An ISBN value, used to uniquely identify books. Two ISBN objects with the same ISBN code are considered equal, and it's immutable.
-
Percentage: An object representing a percentage value, defined as a numerical value between 0 and 100. Equality is based on the numerical value.
-
Currency Exchange Rate: A value object representing the exchange rate between two currencies, defined by the source and target currencies and the exchange rate value. Equality is based on these components.
-
URL: An object representing a URL, defined by its protocol, domain, path, and query parameters. Two URL objects with the same components are considered equal.
-
Temperature Range: A range of temperatures, defined by a minimum and maximum temperature. Equality is based on the equality of these boundaries.
-
Dimension (e.g., Length, Width, Height): Objects representing dimensions, such as the dimensions of a box or a room, are value objects. Equality is based on the values of these dimensions.
-
Point in 2D Space: A point in a 2D coordinate system, defined by x and y coordinates. Equality is determined by the equality of these coordinates.
Each of these examples fits the criteria of immutability, equality based on their values, and lack of unique identity, qualifying them as value objects in the context of Domain-Driven Design.