Builder Design Pattern

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.BuilderDesignPattern

 

static void Main(string[] args)
{
VehicleBuilder builder;

// Create BuildingLine with vehicle builders
BuildingLine buildingLine = new BuildingLine();

// Construct and display vehicles
builder = new ScooterVehicleBuilder();
buildingLine.Construct(builder);
builder.Vehicle.Show();

builder = new CarVehicleBuilder();
buildingLine.Construct(builder);
builder.Vehicle.Show();

builder = new MotorCycleVehicleBuilder();
buildingLine.Construct(builder);
builder.Vehicle.Show();

// Wait for user
Console.ReadKey();
}

class BuildingLine
{
// Builder uses a complex series of steps
public void Construct(VehicleBuilder vehicleBuilder)
{
vehicleBuilder.BuildFrame();
vehicleBuilder.BuildEngine();
vehicleBuilder.BuildWheels();
vehicleBuilder.BuildDoors();
}
}

class CarVehicleBuilder : VehicleBuilder
{
public CarVehicleBuilder()
{
vehicle = new Vehicle(“Car Vehicle”);
}

public override void BuildFrame()
{
vehicle.Frame = “Car Vehicle Frame”;
}

public override void BuildEngine()
{
vehicle.Engine = “3000 CC capicity”;
}

public override void BuildWheels()
{
vehicle.Wheels  = 4;
}

public override void BuildDoors()
{
vehicle.Doors = “4”;
}
}

class MotorCycleVehicleBuilder : VehicleBuilder
{
public MotorCycleVehicleBuilder()
{
vehicle = new Vehicle(“MotorCycle Vehicle”);
}

public override void BuildFrame()
{
vehicle.Frame =  “MotorCycle Vehicle Frame”;
}

public override void BuildEngine()
{
vehicle.Engine =  “300 CC Capacity”;
}

public override void BuildWheels()
{
vehicle.Wheels =  2;
}

public override void BuildDoors()
{
vehicle.Doors =  “0”;
}
}

class ScooterVehicleBuilder : VehicleBuilder
{
public ScooterVehicleBuilder()
{
vehicle = new Vehicle(“Scooter Vehicle”);
}

public override void BuildFrame()
{
vehicle.Frame = “Scooter Vehicle Frame”;
}

public override void BuildEngine()
{
vehicle.Engine = “50 CC Capacity”;
}

public override void BuildWheels()
{
vehicle.Wheels  = 2;
}

public override void BuildDoors()
{
vehicle.Doors = “0”;
}
}

class Vehicle
{

private string _vehicleType;

// Constructor
public Vehicle(string vehicleType)
{
this._vehicleType = vehicleType;
}

public string Frame { get; set; }
public string Engine { get; set; }
public int Wheels { get; set; }
public string Doors { get; set; }

public void Show()
{
Console.WriteLine(“\n—————————“);
Console.WriteLine(“Vehicle Type: {0}”, _vehicleType);
Console.WriteLine(” Frame : {0}”, this.Frame);
Console.WriteLine(” Engine : {0}”, this.Engine);
Console.WriteLine(” #Wheels: {0}”, this.Wheels);
Console.WriteLine(” #Doors : {0}”, this.Doors);
}

}

abstract class VehicleBuilder
{
protected Vehicle vehicle;

// Gets vehicle instance
public Vehicle Vehicle
{
get { return vehicle; }
}

// Abstract build methods
public abstract void BuildFrame();
public abstract void BuildEngine();
public abstract void BuildWheels();
public abstract void BuildDoors();
}

Leave a Reply

Your email address will not be published.