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.
public class Vehicle
{
public string Model { get; set; }
public string Engine { get; set; }
public string Transmission { get; set; }
public string Body { get; set; }
public string FuelCapacity { get; set; }
public string Mileage { get; set; }
public string Displacement { get; set; }
public List<string> Accessories { get; set; }
public Vehicle()
{
Accessories = new List<string>();
}
public void ShowInfo()
{
Console.WriteLine($"{nameof(Model)}: {Model}");
Console.WriteLine($"{nameof(Engine)}: {Engine}");
Console.WriteLine($"{nameof(Body)}: {Body}");
Console.WriteLine($"{nameof(Transmission)}: {Transmission}");
Console.WriteLine($"{nameof(FuelCapacity)}: {FuelCapacity}");
Console.WriteLine($"{nameof(Displacement)}: {Displacement}");
Console.WriteLine($"{nameof(Mileage)}: {Mileage}");
Console.WriteLine($"{nameof(Accessories)}:");
foreach (var accessory in Accessories)
{
Console.WriteLine("\t{0}", accessory);
}
}
}
Creating the abstract/interface Builder class
Here is the code for the IVehicleBuilder.cs file.
public interface IVehicleBuilder
{
void SetModel();
void SetEngine();
void SetTransmission();
void SetBody();
void SetAccessories();
void SetFuelCapacity();
void SetDisplacement();
void SetMileage();
Vehicle GetVehicle();
}
Creating Concrete Builder Classes
Here is the code for the HeroSpendorBuilder.cs file.
public class HeroSplendorBuilder : IVehicleBuilder
{
private readonly Vehicle _vehicle;
public HeroSplendorBuilder()
{
_vehicle = new Vehicle();
}
public Vehicle GetVehicle()
{
return _vehicle;
}
public void SetAccessories()
{
_vehicle.Accessories.Add("Seat Cover");
_vehicle.Accessories.Add("Rear Mirror");
_vehicle.Accessories.Add("Handle cover");
_vehicle.Accessories.Add("Fuel cover");
}
public void SetBody()
{
_vehicle.Body = "Commuter Bikes";
}
public void SetDisplacement()
{
_vehicle.Displacement = "97.2 cc";
}
public void SetEngine()
{
_vehicle.Engine = "4 Stroke";
}
public void SetFuelCapacity()
{
_vehicle.FuelCapacity = "9.8 Ltrs";
}
public void SetMileage()
{
_vehicle.Mileage = "80.6 Kmpl";
}
public void SetModel()
{
_vehicle.Model = "Hero Splendor";
}
public void SetTransmission()
{
_vehicle.Transmission = "120 km/hr";
}
}
Here is the code for the HondaShineBuilder.cs file.
public class HondaShineBuilder : IVehicleBuilder
{
private readonly Vehicle _vehicle;
public HondaShineBuilder()
{
_vehicle = new Vehicle();
}
public Vehicle GetVehicle()
{
return _vehicle;
}
public void SetAccessories()
{
_vehicle.Accessories.Add("Seat Cover");
_vehicle.Accessories.Add("Rear Mirror");
_vehicle.Accessories.Add("Handle cover");
_vehicle.Accessories.Add("Fuel cover");
_vehicle.Accessories.Add("Helmet");
}
public void SetBody()
{
_vehicle.Body = "Commuter Bikes";
}
public void SetDisplacement()
{
_vehicle.Displacement = "123.94 cc";
}
public void SetEngine()
{
_vehicle.Engine = "4 Stroke";
}
public void SetFuelCapacity()
{
_vehicle.FuelCapacity = "10.5 Ltrs";
}
public void SetMileage()
{
_vehicle.Mileage = "60 Kmpl";
}
public void SetModel()
{
_vehicle.Model = "Honda Shine";
}
public void SetTransmission()
{
_vehicle.Transmission = "140 km/hr";
}
}
Creating the Director
Here is the code for the VehicleCreator.cs file.
public class VehicleCreator
{
private readonly IVehicleBuilder _vehicleBuilder;
public VehicleCreator(IVehicleBuilder vehicleBuilder)
{
_vehicleBuilder = vehicleBuilder;
}
public void CreateVehicle()
{
_vehicleBuilder.SetModel();
_vehicleBuilder.SetEngine();
_vehicleBuilder.SetBody();
_vehicleBuilder.SetTransmission();
_vehicleBuilder.SetMileage();
_vehicleBuilder.SetFuelCapacity();
_vehicleBuilder.SetDisplacement();
_vehicleBuilder.SetAccessories();
}
public Vehicle GetVehicle()
{
return _vehicleBuilder.GetVehicle();
}
}
Client Code
Here is the code for the Program.cs file.
var vehicleCreator = new VehicleCreator(new HeroSplendorBuilder());
vehicleCreator.CreateVehicle();
var vehicle = vehicleCreator.GetVehicle();
vehicle.ShowInfo();
Console.WriteLine();
vehicleCreator = new VehicleCreator(new HondaShineBuilder());
vehicleCreator.CreateVehicle();
vehicle = vehicleCreator.GetVehicle();
vehicle.ShowInfo();
Output
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.
public class SuzukiGixxerBuilder : IVehicleBuilder
{
private readonly Vehicle _vehicle;
public SuzukiGixxerBuilder()
{
_vehicle = new Vehicle();
}
public Vehicle GetVehicle()
{
return _vehicle;
}
public void SetAccessories()
{
_vehicle.Accessories.Add("Seat Cover");
_vehicle.Accessories.Add("Rear Mirror");
_vehicle.Accessories.Add("Helmet");
}
public void SetBody()
{
_vehicle.Body = "Sports Bikes";
}
public void SetDisplacement()
{
_vehicle.Displacement = "155 cc";
}
public void SetEngine()
{
_vehicle.Engine = "4 Stroke";
}
public void SetFuelCapacity()
{
_vehicle.FuelCapacity = "12 Ltrs";
}
public void SetMileage()
{
_vehicle.Mileage = "45 Kmpl";
}
public void SetModel()
{
_vehicle.Model = "Suzuki Gixxer";
}
public void SetTransmission()
{
_vehicle.Transmission = "180 km/hr";
}
}
Here is the code for the Program.cs file.
vehicleCreator = new VehicleCreator(new SuzukiGixxerBuilder());
vehicleCreator.CreateVehicle();
vehicle = vehicleCreator.GetVehicle();
vehicle.ShowInfo();
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!