Using Builder Design Pattern in Swift

Using Builder Design Pattern in Swift

I will be discussing the Builder Designer Pattern, how to use them, and their implementation in Swift. Design Pattern falls mainly under the following categories: Creational; Structural; Behavioral

As a developer, we should always follow a design principle that acts as a guide to structuring our code so that it is modular, easy to read, easy to understand, and scalable. In this article, I will be discussing behavioral design pattern, how to use them, and their implementation in Swift. Read on!

Categories of Desing Pattern

Design Pattern falls mainly under the following categories

1. Creational

2. Structural

3. Behavioral

In this article, we will cover the Builder Designer Pattern which is a type of Creational Design Pattern

Builder Design Pattern


We create objects for our classes to leverage the functionality a class provides. Sometimes object creation is simple and can be done by the simple initializer. Other objects might have complicated requirements, for eg, it may require a lot of arguments to initialize the object, which in my opinion is too cumbersome and non-productive. Also, we might need to mix and match these params for initialization. In these cases, we should go for piecewise initialization/construction. For accomplishing that we need an implementation that provides us a step-by-step mechanism so that we have an easier API way of accessing things with granular control of object creation version.

Let’s take an example to understand. Say we want to make an object APIRequest class for our recipe app. This class helps us create request objects to make different API requests. For starters, the current API request class helps us create objects with only two params.

enum Endpoint:String {

case receipesUrl = "/recipes"
case receipeDetail = "/recipes/id"
class ApiRequest {
var endpoint:Endpoint
init(endPoint:Endpoint) {
self.endpoint = endPoint
var apiRequest = ApiRequest(endPoint: .receipesUrl)

Now say you want to pass it in the Http method (POST, GET, etc), headers, URL params, etc. So let’s say we add more parameters to our initializer which may look like this now.

enum HTTPMethod:String {

case get = "GET"
case post = "POST"
enum Endpoint:String {
case receipesUrl = "/recepies"
case receipeDetail = "/recepies/id"
class ApiRequest {
var endpoint:Endpoint
var urlParams:[String:String]
var httpMethod:HTTPMethod
var headers:[String:String]
init(endPoint:Endpoint, httpMethod:HTTPMethod, headers:[String:String], urlParams:[String:String]) {
self.endpoint = endPoint
self.httpMethod = httpMethod
self.headers = headers
self.urlParams = urlParams
var apiRequest = ApiRequest(endPoint: .receipesUrl, httpMethod: .get, headers: [:],urlParams: [:])

As you can see now our initializer has started to grow up. We still need to add a bunch of more parameters like search params, HTTP scheme(HTTP or HTTPS), path parameters, payload, filters, etc. With so many parameters this will become very ugly, plus we then have to handle passing the different params for the initialization which we might not need. We could have a default value or make some of the params optional but still, it is not ideal.

swift mobile-apps programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Hire Mobile App Developers in USA

AppClues Infotech is the best mobile app development company in New York that offers custom mobile app development & design services for Android and iOS.

Top Mobile App Developers & Leading App Development Companies in UK

Profusely examined top Mobile App Development companies in UK with ratings & reviews to help find the best Mobile App Development solution providers.

Mobile App Development North Carolina

Mobile App Development North Carolina In the era of globalization, technology has forced the businesses and industries to jump into the space of competition. Technology has both tangible and intangible benefits that help businesses from different ind...

Top 7 Mobile App Development Companies in Delhi NCR

Looking for a Mobile app development company in Delhi NCR? Here it a list of top mobile app development companies in Delhi for Android & iOS app Development.