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 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 be 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 = object2 assigns to object1 a reference to object2
- value objects are copied by value (cloned): hence the expression valueObject1 = valueObject2 makes a copy of the values of the attributes of valueObject2 to those of valueObject1.
The memory management is also different: if a struct is declared as a local variable in C#, its data is kept in the stack with other local variables, while for classes the data is always stored in the heap memory (as variables only receive a pointer to this memory). This memory management can help in terms of performance.
Value objects represents equality and non-equality defined by business, like date comparison, currency comparison and so on. While struct represents technical implementation by a programming language, to create a proper value object in C# we need to create a class which implements the below four things:-
- Override the equals method and write the equality comparison logic in the equals method.
- Implement operator overloading and write equality and non-equality comparison in "==" and "!=".
- Override "gethashcode" method and ensure that the hash is same for the objects who have same equality.
- Make C# class immutable by removing setters and passing data through the constructors.
Value objects in C++
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 ( object1 = object2).
Value objects in Java
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, though extending support for custom value types is being considered.
Java programmers therefore emulate value objects by creating immutable objects, 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, and declaring all attributes to be of immutable type (such as String, Integer, or any other type declared in accordance with these rules), not of mutable type such an ArrayList or even a Date. 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.
- 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.
- "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.
- koirala, Shivprasad. "Immutable objects in C# - CodeProject". www.codeproject.com. Retrieved 2017-12-26.
- koirala, Shivprasad. "Value Object Design Pattern in C#". www.codeproject.com. Retrieved 2017-12-26.
- "Java Language Specification, chapter 4. Types, Values, and Variables". Retrieved 7 October 2015.
- "JEP 169: Value Objects". Retrieved 7 October 2015.
- "Immutable objects". Collected Java Practices. 2012. Retrieved 5 September 2012.
- hence assignable only in the constructors
- "VALJOs - Value Java Objects". Retrieved 19 October 2014.
- "Project Valhalla". Retrieved 7 August 2018.
- "Evolving the Java Language with @BrianGoetz". Retrieved 7 August 2018.