EMZETT.
Login

Builder pattern

In short: The builder pattern builds complex objects step by step instead of using a constructor with many parameters.

In more detail: A builder collects settings in readable calls (new Pizza.Builder().size(30).cheese().mushrooms().build()) and at the end creates the finished, often immutable object. This avoids confusing constructors with ten parameters.

In Depth

  • When: many optional parameters, objects with complicated composition.
  • Advantages: readable code, checks before creation, immutable results (immutability).
  • Examples: StringBuilder, HTTP client configurations, query builders.
  • In JavaScript: Often replaced by object parameters: new Pizza({ size: 30, cheese: true }).

See also: Factory pattern, Singleton pattern