Builder | Cheat Sheet

Creational Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
3 min readJun 3, 2021

--

The Builder Pattern is often used in situations where you need a wanted order of implementation calls or rather where you want to create complex objects according to a construction plan. Furthermore, the pattern separates the handling of generation and presentation mechanisms.

A big advantage is the monitoring of the creational process (controlled by the Director). It is retrieval only when an object has been created.

Real-life examples

  • Things that do not require any arbitrary order
  • SQL builder
  • Building a house
  • Burger occupy
  • Automotive industry: building a car

Meaning

  • Creation of complex objects according to a construction plan
  • Separate handling of mechanisms of generation and presentation in a single generation process

Applicability

  • Ensuring independence for the creation of complex objects from their components and composition
  • Allowing different display formats of the object that’s generated

Assets and Drawbacks

  • Variable internal representation of a product
  • Isolation of the code in terms of generation and presentation
  • Monitoring of the creation process (controlled by the Director — retrieval only when the object has been created)

Extensibility

  • Products do not need an abstract class, because created classes have a strong variance

Example

First, you need to define the product interface.

Product interface

After that, you define the Builder model in which every build step is defined. Optionally you can define the reset() method to create a new instance of the product model.

Builder

At last, you need to implement the Director that has the responsibility of controlling and monitoring the build steps (creation process).

Director

The client instantiates the Builder and Director to create different products.

Client

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and are a significant introduction into object composition (besides inheritance) and dependency inversion.

Creational Patterns are indispensable when abstracting the instantiation process of objects. Of high importance are encapsulation of concrete classes and hiding the creational process.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links