Builder design pattern lets you construct complex objects step by step, the same construction process can create different representations. Consist of the following items,
Product: represents the complex object under construction.
Builder: specifies an abstract interface for creating parts of a Product object.
Director: constructs an object using the Builder interface.
Sample Code in C#
In this scenario, we develop an application used for hotel booking. We have two room concept served different prices and facilities. Room represents the product in this design pattern. Concept is the builder for getting different room instances. Lastly, we create a director class for getting different room instances according to concepts.
Room class is the main object.
Before creating concrete builder classes, we structure an IBuilder interface.
concept builder classes,
Lastly, by creating the director class we finish the singleton in this scenario.
Let’s use this pattern in a console application.
When running the application we get different room concepts according to FullBoardDeluxeRoomBuilder and HalfBoardStandartRoomBuilder.
Builder design pattern protect us from dirty crowded constructer but we must concrete builders for different instances so increases the workload.
Telescoping Constructor Anti-pattern
If there are a lot of parameters in the constructer, this is a telescoping constructor anti-pattern.