Definition
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
UML class diagram
Participants
The classes and/or objects participating in this pattern are:
- Builder - Specifies an abstract interface for creating parts of a Product object
- ConcreteBuilder -
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
includes classes that define the constituent parts, including interfaces for assembling the parts into the final result
Sample code in C#
The builder pattern describe a way to separate an object from it's construction.The same construction method can create different representation of the object.Use the pattern in the following situations:
You need to construct a complex object which has different representation.
- The algorithm for creating the object should be independent of
- the way the object's parts is assembled.
#region Director
///
/// The contractor is responsible for the
/// house build.
///
class Contractor
{
#region Methods
///
/// Construct a new house with the given
/// house builder
///
///
public void Construct(HouseBuilder houseBuilder)
{
houseBuilder.BuildFloor();
houseBuilder.BuildWalls();
houseBuilder.BuildDoors();
houseBuilder.BuildWindows();
}
#endregion
}
#endregion
#region Builder
abstract class HouseBuilder
{
#region Members
protected House _house;
#endregion
#region Properties
public House House
{
get
{
return _house;
}
}
#endregion
#region Build Methods
public abstract void BuildDoors();
public abstract void BuildWindows();
public abstract void BuildWalls();
public abstract void BuildFloor();
#endregion
}
#endregion
#region Builder Product
class HousePart
{
#region Members
private string _strPartName;
#endregion
#region Properties
///
/// The part name
///
public string PartName
{
get
{
return _strPartName;
}
}
#endregion
#region Ctor
///
/// Construct a new house part with the given
/// name.
///
/// The given name
public HousePart(string name)
{
_strPartName = name;
}
#endregion
}
class House
{
#region Members
private string _material;
private List_houseParts;
#endregion
#region Property
///
/// The building material
///
public string Material
{
get
{
return _material;
}
}
#endregion
#region Ctor
///
/// Construct a new house with the given
/// material
///
/// The given material
public House(string material)
{
_material = material;
_houseParts = new List();
}
#endregion
#region Methods
///
/// Adds a part of the house to the house
///
/// The part to add
public void AddPart(HousePart part)
{
_houseParts.Add(part);
}
#endregion
}
#endregion
#region Builder Concretes
class GlassHouseBuilder : HouseBuilder
{
#region Methods
public override void BuildFloor()
{
_house = new House("glass");
House.AddPart(new HousePart("stone floor"));
}
public override void BuildWalls()
{
House.AddPart(new HousePart(House.Material + " walls"));
}
public override void BuildDoors()
{
House.AddPart(new HousePart("wood doors"));
}
public override void BuildWindows()
{
House.AddPart(new HousePart(House.Material + " windows"));
}
#endregion
}
class WoodHouseBuilder : HouseBuilder
{
#region Methods
public override void BuildFloor()
{
_house = new House("wood");
House.AddPart(new HousePart(House.Material + " floor"));
}
public override void BuildWalls()
{
House.AddPart(new HousePart(House.Material + " floor"));
}
public override void BuildDoors()
{
House.AddPart(new HousePart("iron doors"));
}
public override void BuildWindows()
{
House.AddPart(new HousePart("glass floor"));
}
#endregion
}
#endregion
-
-
Reads(1312)
-
Permalink