1 of 8

Builder Design Pattern �(Creational)

Thanh Le – Software Architect / Technical Consultant

Complexity: 2,5/3

Popularity: 3/3

2 of 8

Intent

  • Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.

��

Image source: internet

3 of 8

Motivation

Imagine a complex object that requires laborious, step-by-step initialization of many fields and nested objects. Such initialization code is usually buried inside a monstrous constructor with lots of parameters. Or even worse: scattered all over the client code.

Image source: internet

You might make the program too complex by creating a subclass for every possible configuration of an object.

4 of 8

Solution

  • The Builder pattern suggests that you extract the object construction code out of its own class and move it to separate objects called builders.
  • The pattern organizes object construction into a set of steps (buildWalls, buildDoor, etc.). To create an object, you execute a series of these steps on a builder object. You can call only those steps that are necessary for producing a particular configuration of an object.

��

Image source: internet

Different builders execute the same task in various ways.

The Builder pattern lets you construct complex objects step by step. The Builder doesn’t allow other objects to access the product while it’s being built.

5 of 8

Solution (cont)

  • You can go further and extract a series of calls to the builder steps you use to construct a product into a separate class called director. The director class defines the order in which to execute the building steps, while the builder provides the implementation for those steps.

  • Having a director class in your program isn’t strictly necessary. You can always call the building steps in a specific order directly from the client code. However, the director class might be a good place to put various construction routines so you can reuse them across your program.

Image source: internet

The director knows which building steps to execute to get a working product.

6 of 8

Structure

Image source: internet

7 of 8

Pros/Cons

  •  Simplifies object creation: The Builder pattern makes it easy to create complex objects by breaking down the creation process into smaller, more manageable steps. This makes the code easier to read and understand, and reduces the risk of errors during object creation.
  • Increases flexibility: The same construction process can create different representations of an object, making it easy to adapt to changing requirements or new use cases.
  • Encapsulates complexity: The Builder pattern encapsulates the complexity of object creation.

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

8 of 8

Practice

https://github.com/thanhle0212/23GoF-Design-Patterns-CSharp