DevOps / Apache Groovy Interview questions
When should you use @Builder instead of a manual builder pattern?
Use @Builder when a class's construction logic is straightforward - just setting a set of properties fluently - and you want the builder boilerplate, a separate builder class with chained setter-like methods and a build() method, generated automatically rather than hand-written and maintained.
Write a manual builder pattern instead when construction needs custom validation logic, conditional steps, or a build process that doesn't map cleanly onto @Builder's generated, mostly one-property-per-method structure.
@Builder also supports several strategies, like the default builder or one working well with @Immutable, so it's worth checking whether one of its built-in strategies actually fits the specific construction pattern needed before deciding a fully manual builder is necessary.
More Related questions...