Builder
On This Lesson 4 topics
The content for this lesson is written and waiting for review. The outline below shows what it covers. It will be published once it is approved.
In this lesson
build objects with many optional parts clearly.
Planned topics
- Problem: a constructor with many parameters is hard to read.
- A builder sets parts by name, then calls
build(). build()validates and returns an immutable object.- Do not use a builder for an object with two fields; a constructor is enough.
- JDK example:
StringBuilder,HttpRequest.newBuilder().
Draft outline details
Demonstration
Order order = Order.builder()
.customer(customerId)
.line(sku, 2)
.coupon("SUMMER10")
.build();Check your understanding
When is a builder too much?
Show answer
For small objects with two or three fields.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.