Design patterns by Tutorials— The power of OOP (part 1)
Faceted and Fluent Builder pattern
Updated on 30th March 2019, 8:00 PM GMT 5:30+
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 Intermediates 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.
Builder design pattern helps us to build an object in a more simpler and readable way. Builder design pattern follows two simple rules as mentioned below.
- Separate the original class representation and its construction methods.
- returns the instance of class in the final step
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 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.
- Retrive 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 above example we can clearly see that construction of 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.
Where to go from here?
Read other parts of this series ‘Design patterns by Tutorials — The power of OOP’
Part-1: Faceted and Fluent Builder pattern in Swift
Part-2: Singleton Design Pattern in Swift
Read my latest series ‘Protocol — The power of Swift’
Read my recent article UndoManager in Swift 5 with example
More parts coming soon…
Liked this article?
Give claps and show your support, Follow my profile
Stay tuned, @hitendrahckr is typing