Files
java-design-patterns/value-object
Ilkka Seppälä 6cd2d0353a docs: Content SEO updates (#2990)
* update yaml frontmatter format

* update abstract document

* update abstract factory

* use the new pattern template

* acyclic visitor seo

* adapter seo

* ambassador seo

* acl seo

* aaa seo

* async method invocation seo

* balking seo

* bridge seo

* builder seo

* business delegate and bytecode seo

* caching seo

* callback seo

* chain seo

* update headings

* circuit breaker seo

* client session + collecting parameter seo

* collection pipeline seo

* combinator SEO

* command seo

* cqrs seo

* commander seo

* component seo

* composite seo

* composite entity seo

* composite view seo

* context object seo

* converter seo

* crtp seo

* currying seo

* dao seo

* data bus seo

* data locality seo

* data mapper seo

* dto seo

* decorator seo

* delegation seo

* di seo

* dirty flag seo

* domain model seo

* double buffer seo

* double checked locking seo

* double dispatch seo

* dynamic proxy seo

* event aggregator seo

* event-based asynchronous seo

* eda seo

* event queue seo

* event sourcing seo

* execute around seo

* extension objects seo

* facade seo

* factory seo

* factory kit seo

* factory method seo

* fanout/fanin seo

* feature toggle seo

* filterer seo

* fluent interface seo

* flux seo

* flyweight seo

* front controller seo

* function composition seo

* game loop seo

* gateway seo

* guarded suspension seo

* half-sync/half-async seo

* health check seo

* hexagonal seo

* identity map seo

* intercepting filter seo

* interpreter seo

* iterator seo

* layers seo

* lazy loading seo

* leader election seo

* leader/followers seo

* lockable object seo

* rename and add seo for marker interface

* master-worker seo

* mediator seo

* memento seo

* metadata mapping seo

* microservice aggregator seo

* api gw seo

* microservices log aggregration seo

* mvc seo

* mvi seo

* mvp seo

* mvvm seo

* monad seo

* monitor seo

* monostate seo

* multiton seo

* mute idiom seo

* naked objects & notification seo

* null object seo

* object mother seo

* object pool seo

* observer seo

* optimistic locking seo

* page controller seo

* page object seo

* parameter object seo

* partial response seo

* pipeline seo

* poison pill seo

* presentation model seo

* private class data seo

* producer-consumer seo

* promise seo

* property seo

* prototype seo

* proxy seo

* queue-based load leveling seo

* reactor seo

* registry seo

* repository seo

* RAII seo

* retry seo

* role object seo

* saga seo

* separated interface seo

* serialized entity seo

* serialized lob seo

* servant seo

* server session seo

* service layer seo

* service locator seo

* service to worker seo

* sharding seo

* single table inheritance seo

* singleton seo

* spatial partition seo

* special case seo

* specification seo

* state seo

* step builder seo

* strangler seo

* strategy seo

* subclass sandbox seo

* table module seo

* template method seo

* throttling seo

* tolerant reader seo

* trampoline seo

* transaction script seo

* twin seo

* type object seo

* unit of work seo

* update method seo

* value object seo

* version number seo

* virtual proxy seo

* visitor seo

* seo enhancements

* seo improvements

* SEO enhancements

* SEO improvements

* SEO additions

* SEO improvements

* more SEO improvements

* rename hexagonal + SEO improvements

* SEO improvements

* more SEO stuff

* SEO improvements

* SEO optimizations

* SEO enhancements

* enchance SEO

* improve SEO

* SEO improvements

* update headers
2024-06-08 19:54:44 +03:00
..
2019-12-07 18:03:49 +02:00
2024-05-23 12:21:54 +03:00
2024-06-08 19:54:44 +03:00

title, shortTitle, description, category, language, tag
title shortTitle description category language tag
Value Object Pattern in Java: Enhancing Performance with Immutable Data Types Value Object Explore the Value Object pattern in Java with our in-depth guide. Learn how immutable objects enhance performance and memory efficiency in software design. Ideal for developers looking to optimize Java applications. Structural en
Data access
Data binding
Domain
Encapsulation
Enterprise patterns
Immutable
Optimization
Performance
Persistence

Also known as

  • Embedded Value
  • Immutable Object
  • Inline Value
  • Integrated Value

Intent of Value Object Design Pattern

The Value Object pattern in Java creates immutable objects that represent a descriptive aspect of the domain with no conceptual identity. It aims to enhance performance and reduce memory overhead by storing frequently accessed immutable data directly within the object that uses it, rather than separately.

Detailed Explanation of Value Object Pattern with Real-World Examples

Real-world example

Consider the case of a business card. In our example, a BusinessCard class is implemented as a Value Object to demonstrate immutable data handling and efficiency in Java applications. In the real world, a business card contains information such as the person's name, job title, phone number, and email address. This information represents a specific and complete set of attributes describing the contact details of an individual but doesn't have an identity itself beyond this information.

In a software system, you can create a BusinessCard class as a Value Object. This class would be immutable, meaning once a BusinessCard object is created with a person's details, those details cannot change. If you need a different business card, you create a new instance rather than modifying the existing one. The equality of two BusinessCard objects would be based on their contained data rather than their memory addresses, ensuring that two business cards with the same details are considered equal. This mirrors how business cards in real life are used and compared based on their content, not on the physical card itself.

In plain words

Value objects are equal when their attributes have the same value.

Wikipedia says

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.

Programmatic Example of Value Object Pattern in Java

There is a class for hero statistics in a role-playing game. The statistics contain attributes such as strength, intelligence, and luck. The statistics of different heroes should be equal when all the attributes are equal.

Here is the HeroStat class that is the value object. Notice the use of Lombok's @Value annotation.

@Value(staticConstructor = "valueOf")
@ToString
class HeroStat {
    int strength;
    int intelligence;
    int luck;
}

The example creates three different HeroStats and compares their equality.

public static void main(String[] args) {
    var statA = HeroStat.valueOf(10, 5, 0);
    var statB = HeroStat.valueOf(10, 5, 0);
    var statC = HeroStat.valueOf(5, 1, 8);

    LOGGER.info("statA: {}", statA);
    LOGGER.info("statB: {}", statB);
    LOGGER.info("statC: {}", statC);

    LOGGER.info("Are statA and statB equal? {}", statA.equals(statB));
    LOGGER.info("Are statA and statC equal? {}", statA.equals(statC));
}

Here's the console output.

20:11:12.199 [main] INFO com.iluwatar.value.object.App - HeroStat(strength=10, intelligence=5, luck=0)
20:11:12.202 [main] INFO com.iluwatar.value.object.App - HeroStat(strength=10, intelligence=5, luck=0)
20:11:12.202 [main] INFO com.iluwatar.value.object.App - HeroStat(strength=5, intelligence=1, luck=8)
20:11:12.202 [main] INFO com.iluwatar.value.object.App - Is statA and statB equal : true
20:11:12.203 [main] INFO com.iluwatar.value.object.App - Is statA and statC equal : false

When to Use the Value Object Pattern in Java

Use the Value Object when

  • Apply the Value Object pattern when you need high-performance Java applications with reduced memory overhead, especially in systems requiring efficient data management.
  • When representing a set of attributes that together describe an entity but without an identity.
  • When the equality of the objects is based on the value of the properties, not the identity.
  • When you need to ensure that objects cannot be altered once created.
  • An application requires high performance and the data involved is immutable.
  • Memory footprint reduction is critical, especially in environments with limited resources.
  • Objects frequently access a particular piece of immutable data.

Value Object Pattern Java Tutorials

Real-World Applications of Value Object Pattern in Java

Benefits and Trade-offs of Value Object Pattern

Benefits:

  • Simplifies code by making objects immutable.
  • Thread-safe as the object's state cannot change after creation.
  • Easier to reason about and maintain.
  • Reduces the memory overhead by avoiding separate allocations for immutable data.
  • Improves performance by minimizing memory accesses and reducing cache misses.

Trade-offs:

  • Creating a new object for every change can be less efficient for complex objects.
  • Increased memory usage due to the creation of multiple objects representing different states.
  • Increases complexity in object design and can lead to tightly coupled systems.
  • Modifying the embedded value necessitates changes across all objects that embed this value, which can complicate maintenance.
  • Factory Method: Often used to create instances of value objects.
  • Flyweight: Shares objects to support large quantities using a minimal amount of memory, somewhat similar in intent but different in implementation.
  • Builder: Can be used to construct complex value objects step by step.
  • Prototype: Can be used to clone existing value objects, though cloning is less common with immutable objects.
  • Singleton: Ensures a class has only one instance and provides a global point of access to it, can be used to manage a shared embedded value.

References and Credits