Builder Pattern In Swift

Marco Del Giudice
2 min readAug 22, 2020

If you’ve ever faced an init with too many parameters or objects whose creation is dictated by dozens of settings, it’s time for you to get to know the builder pattern.

Photo by Ryan Quintal on Unsplash

Intent

The builder pattern is a creational design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming.

The intent of this pattern is to separate the creation of an object from its representation.

This pattern is very popular in Swift because it lets you construct complex objects step by step. In this way the construction process itself can create different representations of the object.

You should apply the Builder design pattern

  • when a constructor has too many parameters;
  • when your need to create different views of a specific object;
  • when you need to compose complex objects.

Problem

A common scenario during the development of an iOS app is to initialize a View and then go to set all the values of the newly created object. Just like this:

--

--

Marco Del Giudice

iOS Engineer @ Keyless📱, code quality enthusiast 👨🏼‍💻. Pizza lover 🍕