Introduction
The Gang of Four (GOF) Design Patterns in .NET's Creational Pattern category includes the Builder Design Pattern. It is employed to construct complex objects step-by-step. It offers a tool for constructing product components. I want to explain what a building pattern is and how it works in this article.
The Builder Design Pattern
GOF claims that the Builder Design Pattern uses a step-by-step methodology and numerous simple components to construct complex ones. To enable the creation of several representations of the same complex item using the same construction process, the process of creating the complex object should be generic.
When creating an object in C# with a large number of necessary and optional fields, the Builder Design Pattern comes in handy, particularly if the object's development process is intricate or if there are multiple potential representations of the item. The goal is to decouple the representation of a complex object from its production so that distinct representations can be produced using the same construction process.
Thus, disentangling the construction process from its depiction is the main goal of the Builder Design Pattern. Only when your object's development is extremely complex do you need to apply the Builder Design Pattern.
The builder design pattern consists of four elements that work together to detach the creation process from its depiction. They are listed below.
- Abstract Builder: The Builder comprises an interface that outlines every step involved in creating the tangible output.
- Concrete Builder: All of the abstract methods are implemented by the Concrete Builder Classes, which also implement the Abstract Builder interface. By putting the Builder interface into practice, the Concrete Builder is in charge of building and assembling the product's component elements. In addition, it maintains and defines the representation it generates.
- Director: The Director establishes the order in which the product is built using the different procedures that the Builder provided.
- Product: The product is a class, and we wish to use the builder design pattern to generate an object of this class. This class defines several components that go into making the final product.
C# Implementation Code
Creating the Product
Here is the code for the Vehicle.cs file.
Creating the abstract/interface Builder class
Here is the code for the IVehicleBuilder.cs file.
Creating Concrete Builder Classes
Here is the code for the HeroSpendorBuilder.cs file.
Here is the code for the HondaShineBuilder.cs file.
Creating the Director
Here is the code for the VehicleCreator.cs file.
Client Code
Here is the code for the Program.cs file.
Output
![Model]()
As an illustration, suppose you need to add a more vehicle builder in the builder design pattern, so here we are adding one more builder SuzukiGixxerBuilder.
Here is the code for the SuzukiGixxerBuilder.cs file.
Here is the code for the Program.cs file.
Output
![Output]()
When should I use it?
Need to design an object using a step-by-step methodology.
The process of creating anything should not be influenced by how its components are put together.
It is necessary to have runtime control over the construction process.
We learned the new technique and evolved together.
Happy coding!