Builder Pattern Code Review

Define the Pattern Interface and Handler Objects

Code Download

  • Download Description:builder pattern download
  • .NET Framework:3.5
  • ..NET Language:C#
  • Date Published:2009-07-01
  • Download Size:9 KB

Code Review

Code Walkthrough

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.