This article is all about “Builder Pattern”. The “Builder Pattern” falls under the category of creation software design pattern.
What is a builder pattern?
It is a pattern to construct a complex object, which is made of a simple objects. Here, the recipe to make the complex object is only known to the director. The builder knows the specifics to create the complex object, and it is hidden from the client. The director calls the builder to build the specifics.
When is the ‘Builder Pattern’ required?
It is required to create a number of objects, whose process of creation is same. However, they differ in the representation. Basically, the construction process/protocol is common, but the content is different. Here, the object is build step by step with the aid of the director.
How a ‘Builder Pattern’ is designed?
- Firstly, set a plan of the object that has the set of common behavioural attributes. This is a sort of ‘Strategy/Behavioural Pattern’.
- Create the concrete class from the strategy/behavioural interface class – point number 1.
- Make an abstract builder, which will set the attributes of the object.
- Extend the abstract builder to create a concrete builder class. This should set the attributes according to the desired concrete class.
- Create a direct or class that will have the builder interface. The director class should have the recipe to make the desired product using the builder reference. The direct or class should also return the final product to the client .
- It is a composition type where the ‘builder’ has the ‘object ’ – composition. Also, the director has the builder reference – aggregation ( builder object – specs is passed by the client ).
Analogy:
The director is like a waiter in the restaurant. The builders are the restaurant staff . A client will speak to director/waiter to order a product. Before ordering to the director, a client will get a specific(menu) from the builder. Also, a client will pass this specific to the waiter/director. Later, a direct or/waiter will prepare (build) the order and will pass to the client. In order to build the customer order, the waiter needs to interact with the builder. It will instruct the builder on what needs to be set.
Problem/scenario: In a Set -top box, we have a different types of tuners. For example, terrestrial and satellite. All these tuners have the same behaviours like – FEC, Polarisation, and modulation. However, the attributes of these behaviours are different. Design a pattern to construct these objects.
Analysis - The terrestrial and satellite objects are having the similar kind of construction. However, they are represented differently. Here, we can use a builder pattern to create these two objects.
Let’s see the UML diagram here.
- The object interface is tuner here. The tuner has the following properties.
a. setTunerFEC()
b. setTunerPolarization()
c. setTunerModulationType()
This is nothing but a behaviour of the object – tuner.
- Create a builder abstract class. a. This should build the TunerFEC(), by calling the tuner behaviour methods – setTunerFEC() b. This should build the TunerPolarization(), by calling the tuner behaviour methods – set TunerPolarization() c. This should build the TunerModulationType(), by calling the tuner behaviour methods – setTunerModulationType()
- Create a concrete builder class – tunerTerrestrialBuilder()
- Create a concrete builder class – tunerSatelliteBuilder()
-
Create a director/Engineer class that will build the final product. This will hold the reference to the builder. With the aid of a builder reference, it will call the specific build. For example, build TunerFEC, build TunerPolarization(), and build setTunerModulationType().
-
Finally, the director will return the final product.
- A client will create a builder type. For example, terrestrial type. Later, it will pass the terrestrial builder object to the director. The director will call the build operations of the terrestrial builder to create a final product. Interface or plan of the tuner object.
Here is the abstract builder class.
Here is the concrete tuner builder – terrestrial type.
Here is the concrete tuner builder – satellite type.
Let’s make a director/Engineer to take up the order of a client.
Now, the main file for a client to test the builder pattern.
Difference between Builder and Factory Pattern.
The factory pattern creates the product in one step. However, the builder
pattern creates the product in multiple steps. The builder pattern uses
composition internally. It is a combination of strategy/behavioural
pattern. Here, the builder object ‘has a’ tuner object.
You can download the entire code here.