Chapter 14. Construction Builder

Incrementally create an immutable object with a builder that stores constructor arguments in fields.

image

14.1 How It Works

The basic recipe for a Construction Builder is very simple. You need to create an immutable object, which I’ll call the product, in a gradual manner. So, take each of the constructor arguments of the product and make a field for each one. Add further fields for any other attributes of the product that you’re collecting. Finally, add a method to create and return a new product object assembled from all the data in the Construction Builder.

You may want to add some lifecycle controls to the Construction ...

Get Domain Specific Languages now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.