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.

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

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 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.
  3. 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.

  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 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.

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.

Feel free to write your thoughts or queries about this blog. I will answer all the queries. Enjoyed this article? Want to support my work? Be my patron.


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’

Part-1: What are type-casting and class-types?
Part-2: Conforming a protocol
Part-3: Protocol composition
Part-4: My rule of thumb for possible data types
Part-5: Protocol as a super-type

Read my recent article UndoManager in Swift 5 with example


Find my repositories on Github
Read my blogs on Medium
Connect me via LinkedIn
Follow me on Twitter


Be my patron

More parts coming soon…

Liked this article?
Give claps and show your support, Follow my profile
Stay tuned, @hitendrahckr is typing