Design patterns by Tutorials— The power of OOP (part 1)

Builder pattern — Faceted and Fluent

Hitendra Solanki
4 min readMar 30, 2019

Updated on 3rd May 2021, 04:10 AM GMT 5:30+

Check out my Card Saver to save your Credit and Debit cards securely.
- Developed in SwiftUI
- only [<1MB] in Size
https://apps.apple.com/US/app/id1565309672?l=en

Many thanks to Uran for this great illustration!

Prerequisites — This blog series requires an intermediate level of expertise in object-oriented programming. You should have basic knowledge about class, object, constructor, inheritance, value and reference type. An intermediate will gain knowledge and experts will sharpen his or her knowledge by reading this series from start to finish.

Design patterns are used to represent the best practices adopted by the experienced object-oriented software developer community.

The builder design pattern helps us to build an object in a more simpler and readable way. The builder design pattern follows two simple rules as mentioned below.

  1. Separate the original class representation and its construction methods.
  2. returns the instance of the class in the final step

The best example of a builder design pattern is SwiftUI, yes you read right. SwiftUI uses builder design pattern for most of its classes e.g. Text, Image

Problem:

Think about the class say Person having ten or more properties, imaging its constructor design when you need to create an instance of the class Person. Its constructor will take ten or more arguments, it will be hard to manage these many arguments as a single function or constructor and eventually, you will lose the readability of code. checkout the below example.

Try to run the above example in the playground, it will run successfully and give you an expected output. Logically it is correct.

We can improve the above example, by overcoming the below points.

  1. We have to pass the values in a mentioned order, can not reorder the parameters sequence to improve the readability.
  2. We have to pass all the values, even if we don’t know some values at the time of object creation.

E.g. Suppose you required to create an object of Person class, but the person is still finding a job. When that person will join any company then only we will have works details.

Solution:

  1. Create logical groups of related properties.
  2. Create separate builder classes for different groups of properties[helps in properties normalization, this is optional].
  3. Retrieve an instance in the final step.

Let’s simplify this with an example,
We already have a class named Person in example WithoutDesignPatternExample1.swift, in which we have 14 properties. If we closely check all the 14 properties, the properties have 4 logical groups.

  1. Personal details properties
  2. Contact details properties
  3. Address details properties
  4. Company details properties

Faceted and Fluent design pattern together helps us to overcome the two problems mentioned above.

In the above example, we have separated the responsibilities of Person class in different classes. Person class now only holds the data properties, while we have created the multiple builder classes having responsibilities to build/update the relative group of properties.

We have one base builder class PersonBuilder and have four more derived builder classes named PersonPersonalDetailsBuilder, PersonContactDetailsBuilder, PersonAddressDetailsBuilder and PersonCompanyDetailsBuilder.

The base class PersonBuilder helps us to switch between multiple builders anytime while other four builders derived from PersonBuilder have responsibilities to update relative properties.

In the above example, we can clearly see that the construction of a Person object is more readable compared to our first example WithoutDesignPatternExample1.swift also we can update the group of properties or any single property at any time in a more readable manner.

In the above example, note that we are returning the builder instance its self after calling every property update method. Which helps us to write chaining of multiple methods of the same builder instead of writing multiple lines separately. This concept is known as Fluent pattern.

Benefits:

  1. Easy initialize an object of a class having too many properties in a more readable manner.
  2. Follows the single responsibility principle.
  3. Initialize object or update properties in any order as per your convenience.

Bonus:

To make the builder pattern consistent across the whole project, you can create protocol as below.

Liked this article?
Give claps and show your support.
It doesn’t cost you anything to clap.

Where to go from here?

Read more blogs from my index page

Find my repositories on Github
Follow me on Twitter

--

--

Hitendra Solanki
Hitendra Solanki

Written by Hitendra Solanki

Software Engineer, {Self-taught Developer by passion, Quick Learner, Mentor, blogger, TeamLead} GitHub: https://github.com/hitendradeveloper