Introduction:
Sometimes you need to build your Classes in such away where its representation is different according to some attributes and this representation of this class needs to be separated from its construction. Builder Design pattern solve this issue by abstract complex object construction from its representation.
Example:
An example of Builder Design pattern is how to assemble vehicles in a step by step way. we can use Vehicle builder to construct the representation of vehicles in a step by step way. this vehicle might be car, truck or even a bicycle. the below code snap shows how to utilize Builder design pattern to construct the different type of vehicles.
static void Main(string[] args) { VehicleBuilder builder; // Create BuildingLine with vehicle builders // Construct and display vehicles builder = new CarVehicleBuilder(); builder = new MotorCycleVehicleBuilder(); // Wait for user class BuildingLine class CarVehicleBuilder : VehicleBuilder public override void BuildFrame() public override void BuildEngine() public override void BuildWheels() public override void BuildDoors() class MotorCycleVehicleBuilder : VehicleBuilder public override void BuildFrame() public override void BuildEngine() public override void BuildWheels() public override void BuildDoors() class ScooterVehicleBuilder : VehicleBuilder public override void BuildFrame() public override void BuildEngine() public override void BuildWheels() public override void BuildDoors() class Vehicle private string _vehicleType; // Constructor public string Frame { get; set; } public void Show() } abstract class VehicleBuilder // Gets vehicle instance // Abstract build methods |