A fluent interface links successive operations through return values, turning a sequence of transformations into a readable expression. With immutable values, each operation returns a new value rather than modifying the receiver. section 2.5
A fluent interface is especially useful when:
- Operations naturally transform one value into another, as with repeated Ball.step calls. section 2.5
- A domain model benefits from readable, composable expressions, such as vector transformations or business rules joined with and and or. section 2.5 section 8.4
- The alternative would obscure the sequence through nested stand-alone calls or verbose function composition. section 2.5
Chaining immutable transformations
A Ball can compute successive states without mutation: ball.step(dt).step(dt).step(dt). Each call returns another Ball, so the final result is distinct from the original while the update sequence remains visible. section 2.5
Composing business rules
A rule algebra can expose default methods such as and and or, allowing expressions like eq(REGION, "EMEA").and(eq(SEGMENT, "Public")).or(not(eq(REGION, "LATAM"))). section 8.4
Listing 2.29 Fluent APIs for free
float dt = 1.0f;
Ball ball = new Ball(new Point(0.0, 0.0), new Vector(1.0, 0.0));
Ball updated = ball
.step(dt) #A
.step(dt) #A
.step(dt); #A
assertNotSame(updated, ball); #B
assertNotEquals(updated, ball); #BShows an immutable Ball transformation whose returned values support readable repeated chaining. section 2.5





