In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.[1] [2]
Examples of value objects are objects representing an amount of money or a date range.
Being small, one can have multiple copies of the same value object that represent the same entity: it is often simpler to create a new object rather than rely on a single instance and use references to it.[2]
Value objects should be immutable:[3] this is required for the implicit contract that two value objects created equal, should remain equal. It is also useful for value objects to be immutable, as client code cannot put the value object in an invalid state or introduce buggy behaviour after instantiation.[4]
Value objects are among the building blocks of DDD.
Due to the nuances of various object-oriented programming languages, each has its own methods and patterns for implementing and using value objects.
In C#, a class is a reference type while a struct (concept derived from the struct in C language) is a value type.[5] Hence an instance derived from a class definition is an object while an instance derived from a struct definition is said to be a value object (to be precise a struct can be made immutable to represent a value object declaring attributes as readonly[6]).
The following procedure can be carried out to add value object properties to a C# class:
Example:
or with a more verbose syntax:
In C++, a value object can be built by overloading the assignment operator and using appropriate constness constraints on the fields (that will be evaluated once by the initializer list of the constructor) and on the methods of the class.
However, if the fields themselves are declared const (rather than use non-const fields while only exposing "getter" accessors), then it won't be possible to fully overwrite such a value object with another .
Python have data classes which provides equality testing and can be made immutable using the frozen
parameter.[9]
@dataclass(frozen=True)class StreetAddress: """Represents a street address."""
street: str city: str
Value objects are available since Java 14, as data records[10]
Unlike C# and C++, Java has no support for custom value types at the language level. Every custom type is a reference type, and therefore has identity and reference semantics,[11] though extending support for custom value types is being considered.[12]
Java programmers therefore emulate value objects by creating immutable objects,[13] because if the state of an object does not change, passing references is semantically equivalent to copying value objects.
A class can be made immutable by declaring all attributes blank final,[14] and declaring all attributes to be of immutable type (such as,, or any other type declared in accordance with these rules), not of mutable type such an or even a . They should also define equals and hashCode to compare values rather than references.
The term "VALJO" (VALue Java Object) has been coined to refer to the stricter set of rules necessary for a correctly defined immutable value object.[15]
Java 14 :
In Kotlin, any class may have a constructor shortcut before the class body (if there is a body at all), which doubles as a declaration of fields and the assignation to those fields. Adding the `data` keyword causes the generation of implementations of `equals` and `hashCode` and the like.