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.
- Separate the original class representation and its construction methods.
- 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
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.
- We have to pass the values in a mentioned order, can not reorder the parameters sequence to improve the readability.
- 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.
- Create logical groups of related properties.
- Create separate builder classes for different groups of properties[helps in properties normalization, this is optional].
- 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.
- Personal details properties
- Contact details properties
- Address details properties
- 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.
- Easy initialize an object of a class having too many properties in a more readable manner.
- Follows the single responsibility principle.
- Initialize object or update properties in any order as per your convenience.
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