Definition
Separates the construction of a complex object from its representation so that the same construction process can create different representations.
Class Diagram
![](https://images.cnblogs.com/cnblogs_com/zhtf2014/Builder2.png)
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![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
private string type;
4
private Dictionary<string, string> parts = new Dictionary<string,string>();
5![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
public Vehicle(string type)
7![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
this.type = type;
9
}
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
public void AddPart (string key, string value)
12![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
parts.Add(key, value);
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
public void Show()
17![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
Console.WriteLine("This is a {0}.", type);
19
foreach (string key in parts.Keys)
20![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
Console.WriteLine("The {0} part is {1}.", key, parts[key]);
22
}
23
Console.WriteLine("------------------------------------");
24
}
25
}
VehicleBuilder
1
abstract class VehicleBuilder
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
protected Vehicle vehicle;
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
public Vehicle Vechicle
6![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return vehicle; }
8
}
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
public MotorcycleBuilder()
4![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
vehicle = new Vehicle("motorcycle");
6
}
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
public override void BuildDoors()
9![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
vehicle.AddPart("doors", "0 doors");
11
}
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
public override void BuildEngine()
14![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
vehicle.AddPart("engine", "500 cc");
16
}
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
public override void BuildFrame()
19![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
vehicle.AddPart("frame", "motorcyle frame");
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
public override void BuildWheels()
24![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
vehicle.AddPart("wheels", "2 wheels");
26
}
27
}
CarBuilder
1
class CarBuilder : VehicleBuilder
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
public CarBuilder()
4![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
vehicle = new Vehicle("car");
6
}
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
public override void BuildDoors()
9![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
vehicle.AddPart("doors", "2 doors");
11
}
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
public override void BuildEngine()
14![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
vehicle.AddPart("engine", "2500 cc");
16
}
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
public override void BuildFrame()
19![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
vehicle.AddPart("frame", "car frame");
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
public override void BuildWheels()
24![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
static void Main(string[] args)
4![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
Shop shop = new Shop();
6
MotorcycleBuilder motorcycleBuilder = new MotorcycleBuilder();
7
CarBuilder carBuilder = new CarBuilder();
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
shop.Construct(motorcycleBuilder);
10
motorcycleBuilder.Vechicle.Show();
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
shop.Construct(carBuilder);
13
carBuilder.Vechicle.Show();
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
Console.Read();
16
}
17
}