Definition
Separates the construction of a complex object from its representation so that the same construction process can create different representations.
Class Diagram

Participants
- Builder.
- specifies an abstract interface for creating parts of a Product object
- ConcreteBuilder.
- constructs and assembles parts of the product by implementing the Builder interface
- defines and keeps track of the representation it creates
- provides an interface for retrieving the product
- Director.
- constructs an object using the Builder interface
- Product.
- represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled
- includes classes that define the constituent parts, including interfaces for assembling the parts into the final result
Example:

Vehicle
1
class Vehicle
2
{
3
private string type;
4
private Dictionary<string, string> parts = new Dictionary<string,string>();
5
6
public Vehicle(string type)
7
{
8
this.type = type;
9
}
10
11
public void AddPart (string key, string value)
12
{
13
parts.Add(key, value);
14
}
15
16
public void Show()
17
{
18
Console.WriteLine("This is a {0}.", type);
19
foreach (string key in parts.Keys)
20
{
21
Console.WriteLine("The {0} part is {1}.", key, parts[key]);
22
}
23
Console.WriteLine("------------------------------------");
24
}
25
}

VehicleBuilder
1
abstract class VehicleBuilder
2
{
3
protected Vehicle vehicle;
4
5
public Vehicle Vechicle
6
{
7
get
{ return vehicle; }
8
}
9
10
public abstract void BuildEngine();
11
public abstract void BuildFrame();
12
public abstract void BuildWheels();
13
public abstract void BuildDoors();
14
}

MotorcycleBuilder
1
class MotorcycleBuilder : VehicleBuilder
2
{
3
public MotorcycleBuilder()
4
{
5
vehicle = new Vehicle("motorcycle");
6
}
7
8
public override void BuildDoors()
9
{
10
vehicle.AddPart("doors", "0 doors");
11
}
12
13
public override void BuildEngine()
14
{
15
vehicle.AddPart("engine", "500 cc");
16
}
17
18
public override void BuildFrame()
19
{
20
vehicle.AddPart("frame", "motorcyle frame");
21
}
22
23
public override void BuildWheels()
24
{
25
vehicle.AddPart("wheels", "2 wheels");
26
}
27
}

CarBuilder
1
class CarBuilder : VehicleBuilder
2
{
3
public CarBuilder()
4
{
5
vehicle = new Vehicle("car");
6
}
7
8
public override void BuildDoors()
9
{
10
vehicle.AddPart("doors", "2 doors");
11
}
12
13
public override void BuildEngine()
14
{
15
vehicle.AddPart("engine", "2500 cc");
16
}
17
18
public override void BuildFrame()
19
{
20
vehicle.AddPart("frame", "car frame");
21
}
22
23
public override void BuildWheels()
24
{
25
vehicle.AddPart("wheels", "4 wheels");
26
}
27
}

Shop
class Shop
{
public void Construct(VehicleBuilder builder)
{
builder.BuildDoors();
builder.BuildEngine();
builder.BuildFrame();
builder.BuildWheels();
}
}

Program
1
class Program
2
{
3
static void Main(string[] args)
4
{
5
Shop shop = new Shop();
6
MotorcycleBuilder motorcycleBuilder = new MotorcycleBuilder();
7
CarBuilder carBuilder = new CarBuilder();
8
9
shop.Construct(motorcycleBuilder);
10
motorcycleBuilder.Vechicle.Show();
11
12
shop.Construct(carBuilder);
13
carBuilder.Vechicle.Show();
14
15
Console.Read();
16
}
17
}