建造者模式可以将一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象。
Director:担任这个角色的类调用具体建造者角色以创建产品对象。
Builder:给出一个抽象接口,以规范产品对象的各个组成成分的建造。如汽车由车轮、方向盘、车厢等等组成。
public void BuildPart()方法给与其子类去实现,就是由子类去创建车轮、方向盘、车厢等等。
ConcreteBuilder:用于实例化操作的子类,继承自Builder。
public void BuildPart()实例化各个部件
public Product GetResult()返回结果
Product:对象。车轮、方向盘、车厢
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();
}
}
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();
}
}