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.
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.
Value objects should be immutable: 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.
Value objects work better if they have native support for copy-by-value semantics, i.e. the expression
valueObject1 = valueObject2
assigns the value of the valueObject1 by creating a copy of the valueObject2, instead of assigning a reference to the second object, as happens in most object oriented languages for assignments among objects.
Value objects are among the building blocks of DDD.
Value objects in C#
In C# a class is a reference type while a struct (concept derived from the struct in C language) is a value type. 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 made immutable to represent a value object declaring attributes as readonly).
The main differences between objects and value objects in C# are that:
- objects are mutable
- value objects are immutable
- objects are referenced and the expression
object1 = object2assigns to
object1a reference to
- value objects are copied by value (cloned): hence the expression
valueObject1 = valueObject2makes a copy of the values of the attributes of
valueObject2to those of
The memory management is also different: the data of a struct in C# is kept in the stack while for classes the data is stored in the heap memory. This memory management can help in terms of performance. This does not happen when the value in question is wrapped inside another object, e.g. an array of some struct type, in which case the struct(s) will live, inside the object, on the heap.
Value Objects in Java
A POJO is a plain old java object and hence an instance of a simple java class.
Hence all these are kinds of mutable Java classes, none of which can be strictly speaking considered a Value object.
In Java it is instead possible to create an immutable object which is close to a Value object: one needs to make the class immutable, by declaring all the attributes blank final and define a copy constructor, and proper hash and equality methods: the object should contain attributes that are derived from immutable classes as well, e.g. a String or an Integer, and not collections or objects with a mutable inner part. 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.
Still objects derived from this class definition would miss the copy-by-value semantics: i.e., with this class one cannot write
valueObject1 = valueObject2, but has to write
valueObject1 = new ValueObject(valueObject2) (invoking the copy constructor).
A formal notion of a "value object" may be added to the Java language in the future. JEP169 is the proposal under current consideration.
Value Objects in C++
In C++ a Value object can be built thanks to the fact that the assignment operator can be overloaded and using appropriate constness constraints to the fields (which will be evaluated once by the initializer list of the constructor) and to the methods of the class.
- Fowler, Martin (2003). "Value Object". Catalog of Patterns of Enterprise Application Architecture. Martin Fowler (martinfowler.com). Retrieved 17 July 2011.
- "Value Object". Portland Pattern Repository's Wiki. Cunningham & Cunningham, Inc. (c2.com). Retrieved 6 September 2012.
- "Value Object Should be Immutable". Portland Pattern Repository's Wiki. Cunningham & Cunningham, Inc. (c2.com). Retrieved 6 September 2012.
- Burns, Sam. "The Value of a Value Object". sam-burns.co.uk.
- "Reference Object". Portland Pattern Repository's Wiki. Cunningham & Cunningham, Inc. (c2.com). Retrieved 21 May 2013.
- "Classes and Structs (C# Programming Guide)". Microsoft Developer Network (msdn.microsoft.com). 2012. Retrieved 5 September 2012.
- "Creating an immutable value object in C# - Part III - Using a struct". Luca Bolognese's WebLog. 2012. Retrieved 7 September 2012.
- ""The Stack Is An Implementation Detail, Part One". Eric Lippert's Blog. 2012. Retrieved 14 December 2012.
- which historically was introduced to enable object handling in a standard way from an IDE, or later from an Application Server in J2EE
- The DTO pattern was used in J2EE in parallel with the DAO pattern, up to the introduction of JPA which replaces Entity beans with annotated POJOs
- the term was introduced to mean go back to simple objects and stop using J2EE Entity beans
- "Immutable objects". Collected Java Practices. 2012. Retrieved 5 September 2012.
- hence assignable only in the constructors
- "VALJOs - Value Java Objects". Retrieved 19 October 2014.