liveBook · Manning

Chapter 4: Domain Modeling with Algebraic Data Types

What you will learn in chapter 4 of Data-Oriented Programming in Java

The chapter introduces an iterative design approach that focuses on domain data and its meaning before operational or system-level concerns.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces an iterative design approach that focuses on domain data and its meaning before operational or system-level concerns.

Key terms: data-oriented design, friction, representation, domain data

4.1 Starting from Requirements

Introduces a rocket-manufacturing checklist as the example and frames the modeling task as turning a loose requirement into precise code.

Key terms: loose requirements statement, precise code, checklist, procedure

4.1.1 The Humble Checklist

A familiar checklist example shows why domain requirements should be clarified before choosing a data model.

Key terms: checklist, repeatability, template, instances

4.1.2 Firming up loose requirements

The section clarifies the difference between reusable checklist templates and the completed checklist instances engineers retain as records.

Key terms: template, instance, instances, data, artifacts

4.1.3 Translating into a model

The section develops a checklist data model by repeatedly sketching types, checking what they mean, and revising them to avoid invalid states.

Key terms: model, meaning, records, Template, Step, Instance, isCompleted, bad states, Status, constraints

4.1.4 New Requirements: Keeping Track of Who Did What

A request to record who completed a checklist step and when exposes how nullable fields can obscure a Step’s meaning and create invalid states.

Key terms: Step, nullable fields, data’s meaning, invalid states, defensive programming, construction, Status

4.1.5 Increasing complexity: another requirements change

A new workflow requirement adds skipped steps to the model and raises the need to understand and record why each step was skipped.

Key terms: skip individual steps, auditable trail, rationale

4.1.6 Beware of refactoring the code, rather than its meaning

This section shows why removing duplicated fields does not fix a domain model whose meaning depends on context, and how that flaw spreads defensive checks throughout the code.

Key terms: Don’t Repeat Yourself (DRY), contextual, illegal states, defensive code, meaning, nullable fields, null pointer exceptions

4.2 Records are tools for modelling “AND”

The section shows how a record’s implicit AND can obscure distinct checklist-status meanings and argues for aligning the code with the meaning of the data.

Key terms: record, AND, semantic drift, completedOn/by, Status, meaning of our data, meaning of our code, checklist status, nullable attributes

4.2.1 Designing consciously with AND

The section uses the attributes of checklist statuses to show how consciously reading a record’s AND relationships leads to separate, more meaningful data types.

Key terms: AND, attributes, Status, Completed, NotStarted, Skipped, illegal states, defensive validation, data model, implementation

4.3 Modeling “one of” relationships with OR

The section identifies the need to group distinct checklist-status records under one data type representing alternative statuses.

Key terms: Checklist Statuses, canonical data type, OR

4.3.1 Interfaces are data types

Shows how an interface and its implementing records can model a domain status with several possible forms, including forms that carry data.

Key terms: interface, records, ChecklistStatus, OR, Enums, attributes

4.4 Some data models should be closed to extension

This section explains why a domain model should allow only the states the domain recognizes, and why an open Java interface can fail to enforce that boundary.

Key terms: ChecklistStatus, NotStarted, Completed, Skipped, closed, interface, extensibility, domain

4.5 Representing Sealed Families of Data

The section shows how Java sealed types represent a closed family of domain data, including when the permits clause can be omitted.

Key terms: sealed, closed families of data, permitted, permits clause, interface's body

4.6 Algebraic Data Types

The section introduces Java records and sealed interfaces as implementations of algebraic data types that can be reasoned about symbolically.

Key terms: Algebraic Data Types, records, sealed interfaces, +, ×

4.6.1 Product Types

This section explains records as product types and shows how their possible states arise from combining the values of their component types.

Key terms: records, Algebraic Product Types, AND, Cartesian product, possible states, invalid states, ANDing

4.6.2 Sum Types

This section explains sum types as OR-shaped choices represented by sealed interfaces in Java, and shows how they combine with product types to build larger data models.

Key terms: Sum Types, OR semantics, sealed interfaces, sum, sealed interface, Product Types, AND semantics, Record Classes

4.6.3 What’s the point of knowing this stuff?

This section explains why algebraic data types are useful beyond Java syntax: they provide a shared vocabulary and a compact way to model meaning.

Key terms: Product Types, Sum Types, sealed interface, underlying algebra, algebra, sketching, modeling, semantics, AND, OR

4.6.4 Refactoring domain models algebraically

The section shows how algebraic laws can reshape a domain model while preserving its information and invariants.

Key terms: algebraic types, product types, sum types, commute, distribute, size, optionality, Empty(), Nothing(), distributive law

4.7 What to do if you you're on older versions of Java

The section explains how to signal a sealed-style family of data types on older Java versions without mistaking that convention for compiler enforcement.

Key terms: sum types, sealed interfaces, domain, related types, same file, sealed qualifier

4.8 Wrapping Up

This section draws together three domain-modeling lessons: align code with the meaning of the data, make implicit AND relationships explicit, and use records and interfaces to express OR choices.

Key terms: illegal states, friction, meaning, implicit ANDs, data modeling, records, interfaces, OR, enum, runtime values

4.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 3: Encoding Semantics in the Type System Chapter 5: Modelling Domain Behaviors →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.