My Journey in Learning Domain-Driven-Design part4 (Value-objects)

What are value objects:

value objects are objects that identify values. and has no unique identity. Value types that measure, quantify, or describe things are easier to create, test, use, optimize, and maintain.

Value objects characteristics:

1- Measures, Quantifies, or Describes:

Value Object is actually a concept that measure, quantifies, or otherwise describes a thing in the domain. A person has an age. Age is not really a thing but measures or quantifies the number of years the person (thing) has lived. A person has a name. The name is not a thing but describes what the person (thing) is called.

2- Immutable:

Once the object has been created we can’t change its state, if we want to we need to replace it by creating a new object with the new state.

  • If the VALUE changes frequently
  • If object creation or deletion is expensive
  • If replacement (rather than modification) will disturb clustering
  • If there is not much sharing of VALUES, or if such sharing is forgone to improve clustering or for some other technical reason

3- Conceptual Whole:

A Value Object may possess just one, a few, or a number of individual attributes, each of which is related to the others. Each attribute contributes an
important part to a whole that collectively the attributes described. Taken apart from the others, each of the attributes fails to provide a cohesive meaning. Only together do all the attributes form the complete intended measure or description.

FullName Value object with primitive type
FullName value object without primitive types

4- Value Equality:

When a Value Object instance is compared to another instance, a test of object
equality is employed. Throughout the system there may be many, many Value
instances that are equal, and yet not the same objects. Equality is determined
by comparing the types of both objects and then their attributes. If both the
types and their attributes are equal.

5- Side-Effect-Free Behavior:

A method of an object can be designed as a Side-Effect-Free Function [Evans].
A function is an operation of an object that produces output but without modifying its own state. The methods of an immutable Value Object must all be Side-Effect-Free Functions because they must not violate its immutability quality.

benefits of value objects:

1- Readability:

Imagine we have a list of phone numbers we would write it like this

const PhoneNumbers: String[] = [...phoneNumbersStrings];
const phoneNumbers: PhoneNumbers[] = [...phoneNumbersValueObjects];

2- self-validation:

As value objects are immutable. there can’t be an invalid value object as it encapsulates its validation. and once it exists it can’t be changed.

3- Reusability & Performance:

We can reuse value objects if they are equal review flyweight pattern.


Value objects are important for Domain-Driven-Design. and I think we can use them outside Domain-Driven-Design too.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store