Builder Pattern Code Review
Code Download
- Download Description:builder pattern download
- .NET Framework:3.5
- ..NET Language:C#
- Date Published:2009-07-01
- Download Size:9 KB
Define the Pattern Interface and Handler Objects
Define the IProductBuilder interface to be used as the "blueprint" for the main Builder classes.
interface IProductBuilder
{
FactoryProduct Product { get; }
void BuildFrame();
void BuildEngine();
void BuildWheels();
void BuildDoors();
}
This interface defines the methods required to construct a specific object.
The MotorCycleBuilder class (object) represents a Builder class.
class MotorCycleBuilder : IProductBuilder
{
public MotorCycleBuilder()
{
this.Product = new FactoryProduct("MotorCycle");
}
public FactoryProduct Product { get; private set; }
public void BuildFrame()
{
this.Product["frame"] = "MotorCycle Frame";
}
public void BuildEngine()
{
this.Product["engine"] = "500 cc";
}
public void BuildWheels()
{
this.Product["wheels"] = "2";
}
public void BuildDoors()
{
this.Product["doors"] = "0";
}
}
This class inherits from the IProductBuilder interface, and therefore implements the required methods to construct a MotorCycle object.
The VehicleBuilder class (object) represents a Builder class.
class VehicleBuilder : IProductBuilder
{
public VehicleBuilder()
{
this.Product = new FactoryProduct("Vehicle");
}
public FactoryProduct Product { get; private set; }
public void BuildFrame()
{
this.Product["frame"] = "Vehicle Frame";
}
public void BuildEngine()
{
this.Product["engine"] = "2500 cc";
}
public void BuildWheels()
{
this.Product["wheels"] = "4";
}
public void BuildDoors()
{
this.Product["doors"] = "4";
}
}
This class inherits from the IProductBuilder interface, and therefore implements the required methods to construct a Vehicle object.
The ScooterBuilder class (object) represents a Builder class.
class ScooterBuilder : IProductBuilder
{
public ScooterBuilder()
{
this.Product = new FactoryProduct("Scooter");
}
public FactoryProduct Product { get; private set; }
public void BuildFrame()
{
this.Product["frame"] = "Scooter Frame";
}
public void BuildEngine()
{
this.Product["engine"] = "50 cc";
}
public void BuildWheels()
{
this.Product["wheels"] = "2";
}
public void BuildDoors()
{
this.Product["doors"] = "0";
}
}
This class inherits from the IProductBuilder interface, and therefore implements the required methods to construct a Scooter object.
Define the Factory Director and Factory Product Objects
The FactoryDirector class (object) represents a Director class.
class FactoryDirector
{
public void Construct(IProductBuilder builder)
{
builder.BuildFrame();
builder.BuildEngine();
builder.BuildWheels();
builder.BuildDoors();
}
}
This class invokes the methods associated with a specific Builder class to construct a particular object.
The FactoryProduct class (object) defines the details stored about a particular product.
class FactoryProduct
{
private StringDictionary parts = new StringDictionary();
private String productType = String.Empty;
public FactoryProduct(String type)
{
this.productType = type;
}
public String this[String key]
{
get
{
return this.parts[key];
}
set
{
this.parts[key] = value;
}
}
public void DisplayProduct()
{
Console.WriteLine("\n");
Console.WriteLine("Product Type: {0}", this.productType);
Console.WriteLine("\tFrame: {0}", this.parts["frame"]);
Console.WriteLine("\tEngine: {0}", this.parts["engine"]);
Console.WriteLine("\tWheels: {0}", this.parts["wheels"]);
Console.WriteLine("\tDoors: {0}", this.parts["doors"]);
}
}
Use the Builder Pattern
The Program class creates an instance of the FactoryDirector class, and creates instances of the different Builder classes.
class Program
{
static void Main(string[] args)
{
FactoryDirector director = new FactoryDirector();
ScooterBuilder sctBuilder = new ScooterBuilder();
VehicleBuilder vchBuilder = new VehicleBuilder();
MotorCycleBuilder mcyBuilder = new MotorCycleBuilder();
director.Construct(sctBuilder);
sctBuilder.Product.DisplayProduct();
director.Construct(vchBuilder);
vchBuilder.Product.DisplayProduct();
director.Construct(mcyBuilder);
mcyBuilder.Product.DisplayProduct();
Console.Read();
}
}
The Director class (object) constructs each builder object (product) and displays required output.