liveBook · Manning

Lifecycle

As explained in Data-Oriented Programming in Java

A lifecycle is the sequence of states and changes an entity undergoes over the course of its existence.

A lifecycle is the meaningful sequence of states an entity passes through and the transitions that move it between them. It is easy to leave that sequence implicit in flags, nullable fields, and nested conditionals; doing so makes readers reconstruct business meaning from assignments scattered across a codebase. Make lifecycle states explicit when they affect what an entity is, what may happen next, or which operations are valid. section 1.3.1 section 5.3.1

Consider invoicing. An invoice is open when issued and closed when paid. Its due date and a customer's rating determine whether it is past due, while invoice numbering and delivery require coordination with a central billing service. A late-fee invoice therefore has both a business lifecycle and a software-facing one: it can exist first as a draft, then become official only after submission succeeds. section 5.1.1 section 5.3.1

Model a lifecycle as data rather than letting its meaning hide in control flow:

  • Name each state for the fact it represents, such as Draft, Billed, Rejected, or InReview; state-specific data can travel with that state, including an invoice ID, rejection reason, or approval ID. section 5.4.3
  • Express transitions in the data flow: invoices become past-due items, then draft late fees, then either billed or rejected fees, or fees awaiting approval. section 5.3.1 section 5.4.3
  • Accept duplication when separate state types make invalid operations unrepresentable. A method accepting only a draft fee states, and lets the compiler enforce, that only drafts can be assessed. section 5.4.3
  • Evaluate the tradeoff when common operations span all states. Collapsing states into a runtime Lifecycle field makes aggregation convenient, but requires defensive checks to prevent invalid transitions. section 5.4.3

Late fees: types encode the transition

A generic representation can retain shared late-fee fields while preserving lifecycle information: LateFee<Draft>, LateFee<Billed>, LateFee<Rejected>, and LateFee<InReview>. Behaviors can require the state they need—for example, an operation can accept LateFee<Draft>—while code that genuinely handles every phase can accept LateFee<? extends Lifecycle>. This restores an expressive pipeline without forcing every shared operation to pattern-match separate record types. section 5.4.3

Lifecycle modeling is not only for production workflows. Tests can represent the meaningful states of a system as data and iterate over them, rather than write one mock-heavy test per branch. For a mirror synchronization service, EMPTY, STALE, and UP_TO_DATE describe the world under test more directly than a collection of branch-specific test names. The result is a test that checks domain behavior, not merely whether a branch ran. section 11.3.4

Listing 5.39 Restoring expressivity in our data flow
List<Invoice>
-> List<PastDue>   
-> LateFee<Draft>                                       #A
-> BillableFee -> LateFee<Billed> OR LateFee<Rejected>  #A
-> NeedsApproval -> LateFee<InReview>                   #A

This pipeline shows lifecycle states in the types flowing between transformations, including the billed, rejected, and approval paths. section 5.4.3

How Data-Oriented Programming in Java covers it

How deep it goes
Works through state-specific records, runtime state tradeoffs, and generic state parameters; defers a single universally best representation.
Examples
Uses open and closed invoices, draft late fees, billed or rejected outcomes, approval review, and mirror synchronization states.
What it assumes
Assumes intermediate Java and uses records, sealed hierarchies, generics, streams, and pattern matching to refine the model.
What you can do after
Build it from scratch by encoding states and transitions as data types that constrain valid operations.
How it connects
Connects lifecycle modeling to records, sealed classes, generics, pattern matching, type safety, and data-oriented testing.

Also explained in

Compare how 8 books explain lifecycle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.