Builder Pattern in Swift iOS

Tram Ho

Difficulty: Beginner | Easy | Normal | Challenging
Xcode 14.0.1
Swift 5.7

1. About

In this article, I will introduce to you a Fundamental Design Pattern – Builder Pattern. So what is the Builder Pattern like, its application and how to install it?

As usual, I won’t talk about theory/definition right away. I will show an example of the Builder Pattern first, then we will dive into its essence!!!

2. Make a problem

I have an example of creating a URLRequest for calling API as follows:

Don’t worry if you can’t understand the entire code above. The main content of the above code is that we have a class URLRequestHelper that has a build() function that returns a URLRequest for us. We just need to get the result (this URLRequest) to make the API call:

So we will get the list of movies from API

So what’s wrong with our URLRequestHelper class? And why do I mention it in this article.
=> Of course our URLRequestHelper class is perfectly fine, we can use it in most projects.
Suppose in a certain project, I want to add cachePolicy (URLRequest.CachePolicy) or timeoutInterval (TimeInterval),… how about our init function?

The init function looks fine, right?

  • What if I need to add more parameters? So our init function will become a lot complicated, right?
  • What if I only need to use queryItems (like the API movie call example above)? (while we need to provide multiple parameters to the init function)
    => Of course, since we already have the default value , we only need to pass the necessary parameters (that’s why the Builder Pattern can be anti-pattern, but we’ll discuss this later)

=> That’s why we have the Builder Pattern to help reduce the complexity of our initialization function (init).
=> Let’s apply the Builder Pattern to the example code above

3. Example of Builder Pattern

Still an example of creating a URLRequest to call the API :

It’s much easier to understand, isn’t it, people. I can get the URLRequest from the build() function as follows:

Back to the problem in part 2, suppose we need to add other parameters to build the URLRequest for example: cachePolicy (URLRequest.CachePolicy) or timeoutInterval (TimeInterval), ..
We just need to create the corresponding functions, for example:

Very simple, isn’t it, people. Not only that, the Builder Pattern also helps us to comply with the S principle (in SOLID) – each function has only one task.

So we’ve gone through an example of using the Builder Pattern. We will come to the definition / theory of the Builder Pattern

3. What is the Builder Pattern?

  • The Builder Pattern belongs to the Creational Design Pattern group. It will help us to create a complex object by providing each parameter, and they also increase the reusability of functions that provide parameters.
  • The builder pattern allows you to create complex objects by providing inputs step- by-step, instead of requiring all inputs upfront via an initializer – Design Pattern by Tutorials
  • 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 – Dive Into Design Pattern

Those are some definitions of the Builder Pattern. We have the following diagram: Builder_Diagram.png

  • Builder Pattern it will include 3 parts:
    • Product : Is the complex class/object that we need to instantiate (in example 2, our Product is URLRequest )
    • A Builder class: It is a place to contain functions that provide parameters and a build() function that returns Product for us (here, returns URLRequest )
    • Director : It can be a UIViewController or a Helper class. Where we initialize the Builder class to receive the Product (in example 2, our Director is the place to execute the code that provides the path and queryItems )

4. When to use the Builder Pattern? ️

  • As mentioned above, we use the Builder Pattern to reduce the complexity when instantiating a complex class/object and we don’t need to care about the order in which the parameters are provided (like in example 2, we can provide path before queryItems or vice versa)
  • Use the builder pattern when you want to create a complex object using a series of steps. – Design Patterns by Tutorials
  • Use the Builder pattern to get rid of a “telescopic constructor” – Dive Into Design Pattern
  • Use the Builder pattern when you want your code to be able to create different representations of some product – Dive Into Design Pattern
  • Use the Builder to construct Composite trees or other complex objects – Dive Into Design Pattern

5. Pros and cons

5.1. Advantage

  • We can initialize the object step by step and don’t care the order of those steps
  • Functions that provide parameters can be reused
  • True to the S principle (in SOLID) – Single Responsibility Principle

5.2. Defect

  • The complexity of the code can increase when we need to create many new classes (provide more parameters => complexity of the Builder class increases)

6. Conclusion

  • So we have learned what the Builder Pattern is and how to install it through this blog.
  • If you have any questions or have a way to optimize your Builder Pattern example, don’t hesitate to comment. Thank you so much = Thank a lot = Thank you very much.

Some good examples you can refer to:


Share the news now

Source : Viblo