HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includi ...
, a value object is a small
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
that represents a ''simple''
entity An entity is something that exists as itself, as a subject or as an object, actually or potentially, concretely or abstractly, physically or not. It need not be of material existence. In particular, abstractions and legal fictions are usually ...
whose equality is not based on
identity Identity may refer to: * Identity document * Identity (philosophy) * Identity (social science) * Identity (mathematics) Arts and entertainment Film and television * ''Identity'' (1987 film), an Iranian film * ''Identity'' (2003 film), ...
: 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.


Implementation

Due to the nuances of various
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of p ...
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s, each has its own methods and
patterns A pattern is a regularity in the world, in human-made design, or in abstract ideas. As such, the elements of a pattern repeat in a predictable manner. A geometric pattern is a kind of pattern formed of geometric shapes and typically repeated li ...
for implementing and using value objects.


C#

In C#, a class is a
reference type In computer programming, data types can be divided into two categories: value types (or by-value types) and reference types (or by-reference types). Value types are completely represented by their meaning, while reference types are references to ano ...
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 following procedure can be carried out to add value object properties to a C# class: # Override th

method to ensure the object is compared using
business logic In computer software, business logic or domain logic is the part of the program that encodes the real-world business rules that determine how data can be created, stored, and changed. It is contrasted with the remainder of the software that might ...
# Operator overload the default behavior of and to use the method. # Override th

method and ensure that the hash is same for the objects who have same equality. # Make the class immutable by removing any property setters and only passing member values through the constructors.


C++

In C++, a value object can be built by overloading the
assignment operator Assignment, assign or The Assignment may refer to: * Homework * Sex assignment * The process of sending National Basketball Association players to its development league; see Computing * Assignment (computer science), a type of modification t ...
and using appropriate constness constraints on the fields (that will be evaluated once by the
initializer list In computer programming, initialization (or initialisation) is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on the programming language, as well as the type, storage class ...
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 ().


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 , , 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. Value objects are available since Java 14, as data records


Example

public class StreetAddress


See also

*
Data transfer object In the field of programming a data transfer object (DTOMSDN (2010). Data Transfer Object. Microsoft MSDN Library. Retrieved from https://msdn.microsoft.com/en-us/library/ms978717.aspx.Fowler, Martin (2010). Data Transfer Object. Patterns of Enterpri ...
*
Domain-driven design Domain-driven design (DDD) is a major software design approach, focusing on modeling software to match a domain according to input from that domain's experts. Under domain-driven design, the structure and language of software code (class name ...
* Value semantics


References

{{Reflist Object (computer science)