Skip to content
Commitwise
Menu

Builder pattern

Also called: Builder

A design pattern for creating an object step by step, setting only the parts you need, and validating it at the end.

Definition

A builder collects the settings for an object through named methods and creates the finished object in one final build() call. It replaces long constructors with many optional parameters, keeps the final object immutable, and gives one place to check that the combination of settings is valid. In Kotlin and Swift, named and default arguments often make a builder unnecessary.

In practice

OkHttpClient.Builder().connectTimeout(10, SECONDS).addInterceptor(auth).build() configures only what differs from the defaults.

Related terms

Esc

Type to search the whole site.