DEV Community

Saras Growth Space
Saras Growth Space

Posted on

LLD Domain Modeling: Value Objects (Objects Defined by Meaning, Not Identity)

In the previous post, we learned about Entities.

Entities are objects whose identity matters.

For example:

Order #123
Ride #567
Booking #ABC
Enter fullscreen mode Exit fullscreen mode

Even if their data changes, they remain the same business object because their identity remains the same.

But not every object in a system behaves this way.

Some objects are important not because of who they are, but because of what they represent.

These are called:

Value Objects

Understanding the difference between Entities and Value Objects is one of the most important skills in Domain Modeling.


Start With a Simple Question

Imagine two money objects:

₹500
Enter fullscreen mode Exit fullscreen mode

and

₹500
Enter fullscreen mode Exit fullscreen mode

Do we care which specific ₹500 object it is?

No.

If the value is the same, they mean the same thing.

Now compare this with Orders:

Order #101
Order #102
Enter fullscreen mode Exit fullscreen mode

Even if both have:

Amount = ₹500
Enter fullscreen mode Exit fullscreen mode

they are still different orders.

Why?

Because identity matters.

This is the core distinction.


What Is a Value Object?

A Value Object is:

an object whose meaning comes entirely from its values, not from its identity.

If two value objects contain the same values:

they are considered equal.


Real-Life Example

Think about a postal address:

221B Baker Street
London
NW1
Enter fullscreen mode Exit fullscreen mode

If two address objects contain exactly the same information:

they represent the same address.

Nobody asks:

Which Address object is this?
Enter fullscreen mode Exit fullscreen mode

Instead they ask:

What address does it represent?
Enter fullscreen mode Exit fullscreen mode

That makes Address a Value Object.


Entity vs Value Object

Consider this:

User

User #123
Enter fullscreen mode Exit fullscreen mode

Identity matters.

Even if:

  • email changes
  • phone changes
  • address changes

it is still the same user.

Entity.


Address

Street
City
State
ZIP
Enter fullscreen mode Exit fullscreen mode

Identity does not matter.

Only values matter.

Value Object.


The Quick Test

Whenever you encounter an object, ask:

If two instances contain exactly the same data, should the business consider them the same?

If YES:

Value Object
Enter fullscreen mode Exit fullscreen mode

If NO:

Entity
Enter fullscreen mode Exit fullscreen mode

Common Value Object Examples

Across real systems:

Money

₹500
Enter fullscreen mode Exit fullscreen mode

Usually represented by:

  • amount
  • currency

Address

Street
City
Country
ZIP
Enter fullscreen mode Exit fullscreen mode

Location

Latitude
Longitude
Enter fullscreen mode Exit fullscreen mode

Coordinates

x
y
Enter fullscreen mode Exit fullscreen mode

Time Range

Start Time
End Time
Enter fullscreen mode Exit fullscreen mode

Distance

5 km
Enter fullscreen mode Exit fullscreen mode

These are usually Value Objects.


Why Value Objects Matter

Many beginners model everything as entities.

Example:

AddressEntity
MoneyEntity
LocationEntity
Enter fullscreen mode Exit fullscreen mode

This creates unnecessary complexity.

Because these objects do not need:

  • identity
  • lifecycle
  • tracking

They only need meaning.


Value Objects Make Models Simpler

Imagine an Order:

Without Value Objects:

Order
    street
    city
    state
    zip
    amount
    currency
Enter fullscreen mode Exit fullscreen mode

The entity starts becoming cluttered.

With Value Objects:

Order
    Address
    Money
Enter fullscreen mode Exit fullscreen mode

The model becomes:

  • cleaner
  • more expressive
  • easier to maintain

Value Objects Should Usually Be Immutable

This is one of their most important characteristics.

Suppose we have:

Money(500, INR)
Enter fullscreen mode Exit fullscreen mode

Changing it into:

Money(1000, INR)
Enter fullscreen mode Exit fullscreen mode

creates a completely different value.

Instead of modifying the existing object:

create a new one.

This prevents accidental side effects.


Why Immutability Helps

Imagine:

Address
Enter fullscreen mode Exit fullscreen mode

being shared by multiple entities.

If someone changes it unexpectedly:

multiple objects may be affected.

Immutable value objects avoid this problem.

Their meaning never changes after creation.


Equality Works Differently

Entities:

Compare by identity
Enter fullscreen mode Exit fullscreen mode

Value Objects:

Compare by values
Enter fullscreen mode Exit fullscreen mode

Example:

Money(500, INR)
Enter fullscreen mode Exit fullscreen mode

and

Money(500, INR)
Enter fullscreen mode Exit fullscreen mode

should be equal.

Because their values are identical.


Ride Sharing Example

Consider Uber.

Ride:

Ride #567
Enter fullscreen mode Exit fullscreen mode

Entity.


Pickup Location:

Latitude
Longitude
Enter fullscreen mode Exit fullscreen mode

Value Object.


Fare:

Amount
Currency
Enter fullscreen mode Exit fullscreen mode

Value Object.


Notice:

The Ride is tracked.

The Location and Fare simply describe the Ride.


Common Beginner Mistake

Treating every noun as an entity.

Example:

LocationEntity
PriceEntity
AddressEntity
DistanceEntity
Enter fullscreen mode Exit fullscreen mode

This usually creates:

  • unnecessary IDs
  • unnecessary databases
  • unnecessary lifecycle management

Not everything needs identity.


Strong Domain Modeling Thinking

When designing a model:

Ask:

Does the business care who this object is,
or only what value it represents?
Enter fullscreen mode Exit fullscreen mode

That single question often reveals whether you're looking at an Entity or a Value Object.


The Most Important Insight

Entities represent:

identity through time
Enter fullscreen mode Exit fullscreen mode

Value Objects represent:

meaning through values
Enter fullscreen mode Exit fullscreen mode

Understanding this distinction is one of the foundations of strong Low-Level Design.


One-Line Takeaway

A Value Object is an object whose meaning comes entirely from its values, making identity irrelevant and equality based solely on the data it contains.

Top comments (0)