Session

Let's Introduce Value Classes into an Existing DDD Project

When modeling a domain concept, Domain-Driven Design (DDD) advocates for the use of value objects over entities whenever possible. Their immutability and lack of state make value objects a far simpler concept, and their strong typing moves an entire class of potential bugs from runtime to compile-time. However, this approach is at risk of 'type explosion,' where types with just a single `String` are wrapped in yet another type, drastically increasing the number of types in a project and, with it, memory consumption.

JEP 401 (from Project Valhalla) proposes _value classes_, whose instances lack object identity and can contain final fields only. This allows the JVM to store them in a flattened layout on the stack, making their memory consumption (and performance) very similar to primitives. Now that JEP 401 is targeted for JDK 28, we can look forward to representing DDD's value objects more efficiently very soon, due to the extra memory efficiency and performance benefits, potentially solving the memory consumption problem.

Let's put that to the test in this talk, by introducing value classes in an existing DDD project using a fun domain and a JDK 28 early-access build. We'll cover benchmarking results, the pros and cons of this approach, plenty of value class characteristics, and the status of Valhalla's other features. You'll walk away with a good understanding of value classes, their related features, and when (not) to use them.

Hanno Embregts

Java Developer by day, musician by night. A bit of both at conferences.

Leusden, The Netherlands

Actions

Please note that Sessionize is not responsible for the accuracy or validity of the data provided by speakers. If you suspect this profile to be fake or spam, please let us know.

Jump to top