Skip to main content

Command Palette

Search for a command to run...

Builder Design Pattern

Design Patterns Serie

Published
2 min read
Builder Design Pattern
M
A passionate software engineer who is interested in solving challenging problems, gaining new experiences, building exciting projects, and technical writing.

Intent

It builds a complex object using simple objects and using a step-by-step approach. It allows producing different representations, and types of an object using the same construction code.

Problem

If we have a complex object that requires exhausting step-by-step initialization of many fields. this initialization code is often buried inside a constructor with lots of parameters. So we create the elements of a complex aggregate.

Solution

The builder design pattern suggests extracting the object construction code out of its own class and moving it to separate objects called "builders".
To create an object, we must execute a series of steps on a builder object. We don’t have to call all the steps. We only call the steps that are necessary for producing a particular configuration of the object we want to create.

Pros and Cons

Pros

  • Encapsulates code for construction and representation.
  • Allows us to vary a product's internal representation.
  • We can reuse the same construction code when building various representations of products.

Cons

  • The complexity of the code increases as the pattern requires creating multiple new classes.

Thank you, and goodbye.

Design Patterns

Part 6 of 13

Different design patterns are explained in a simple fun way with examples based on the book "Head First Design Patterns" as a reference.

Up next

Strategy Design Pattern

Design Patterns Serie