五、建造者模式的實現:
下面的程序代碼演示了Shop對象使用VehicleBuilders來建造不同的交通工具。該例子使用了Builder模式順序建造交通工具的不同部分。
// Builder pattern -- Real World example
using System;
using System.Collections;
// "Director"
class Shop
{
// Methods
public void Construct( VehicleBuilder vehicleBuilder )
{
vehicleBuilder.BuildFrame();
vehicleBuilder.BuildEngine();
vehicleBuilder.BuildWheels();
vehicleBuilder.BuildDoors();
}
}
// "Builder"
abstract class VehicleBuilder
{
// FIElds
protected Vehicle vehicle;
// PropertIEs
public Vehicle Vehicle
{
get{ return vehicle; }
}
// Methods
abstract public void BuildFrame();
abstract public void BuildEngine();
abstract public void BuildWheels();
abstract public void BuildDoors();
}
// "ConcreteBuilder1"
class MotorCycleBuilder : VehicleBuilder
{
// Methods
override public void BuildFrame()
{
vehicle = new Vehicle( "MotorCycle" );
vehicle[ "frame" ] = "MotorCycle Frame";
}
override public void BuildEngine()
{
vehicle[ "engine" ] = "500 cc";
}
override public void BuildWheels()
{
vehicle[ "wheels" ] = "2";
}
override public void BuildDoors()
{
vehicle[ "doors" ] = "0";
}
}
// "ConcreteBuilder2"
class CarBuilder : VehicleBuilder
{
// Methods
override public void BuildFrame()
{
vehicle = new Vehicle( "Car" );
vehicle[ "frame" ] = "Car Frame";
}
override public void BuildEngine()
{
vehicle[ "engine" ] = "2500 cc";
}
override public void BuildWheels()
{
vehicle[ "wheels" ] = "4";
}
override public void BuildDoors()
{
vehicle[ "doors" ] = "4";
}
}
// "ConcreteBuilder3"
class ScooterBuilder : VehicleBuilder
{
// Methods
override public void BuildFrame()
{
vehicle = new Vehicle( "Scooter" );
vehicle[ "frame" ] = "Scooter Frame";
}
override public void BuildEngine()
{
vehicle[ "engine" ] = "none";
}
override public void BuildWheels()
{
vehicle[ "wheels" ] = "2";
}
override public void BuildDoors()
{
vehicle[ "doors" ] = "0";
}
}
// "Product"
class Vehicle
{
// FIElds
private string type;
private Hashtable parts = new Hashtable();
// Constructors
public Vehicle( string type )
{
this.type = type;
}
// Indexers
public object this[ string key ]
{
get{ return parts[ key ]; }
set{ parts[ key ] = value; }
}
// Methods
public void Show()
{
Console.WriteLine( " ---------------------------");
Console.WriteLine( "Vehicle Type: "+ type );
Console.WriteLine( " Frame : " + parts[ "frame" ] );
Console.WriteLine( " Engine : "+ parts[ "engine"] );
Console.WriteLine( " #Wheels: "+ parts[ "wheels"] );
Console.WriteLine( " #Doors : "+ parts[ "doors" ] );
}
}
/**//// <summary>
/// BuilderApp test
/// </summary>
public class BuilderApp
{
public static void Main( string[] args )
{
// Create shop and vehicle builders
Shop shop = new Shop();
VehicleBuilder b1 = new ScooterBuilder();
VehicleBuilder b2 = new CarBuilder();
VehicleBuilder b3 = new MotorCycleBuilder();
// Construct and display vehicles
shop.Construct( b1 );
b1.Vehicle.Show();
shop.Construct( b2 );
b2.Vehicle.Show();
shop.Construct( b3 );
b3.Vehicle.Show();
}
}