Wednesday, December 19, 2007 1:38 PM
kolbis
Builder
Real World Example
I had to create an object that represents a house. The house got windows, doors, kitchen and etc. Each house object can vary and use different type of interior. Beside that there are several house types; there is a Vila, Duplex and etc.
My problem is how to create that object?
Builder
As mentioned before, I had to "create" a house, thus the Builder design pattern falls into the Creational design pattern category. Like the Factory design pattern, it abstracts the instantiation process. The critical distinction between the Builder and Factory patterns is that Builder is used to separate the representation of data and the construction logic of complex objects.
Here is a Class Diagram that describes it:
The code will look as follows:
The first class is the abstract builder that represents the creation for the complex object, in my case this is the House. This class contains the steps required for building a house, however it does not contain the implementation on how to build the house. The steps are those abstract methods that the class defines.
public abstract class AbstractHouseBuilder
{
public object House { get; set; }
public AbstractHouseBuilder()
{
}
public abstract void BuildWindows();
public abstract void BuildRoof();
public abstract void BuildFloor();
//more abstraction methods goes here
public virtual object GetHouse()
{
return House;
}
}
Deriving from the AbstractHouseBuilder is the concrete implementation of the steps. The following classes represenets different implementation for building windows, doors, roofs and etc.
public class DuplexBuilder : AbstractHouseBuilder
{
public override void BuildWindows()
{
//implement windows for duplex...
}
public override void BuildRoof()
{
//implement roof for duplex...
}
public override void BuildFloor()
{
//implement foor for duplex...
}
}public class VilaBuilder : AbstractHouseBuilder
{
public override void BuildWindows()
{
//implement windows for vila...
}
public override void BuildRoof()
{
//implement roof for vila...
}
public override void BuildFloor()
{
//implement foor for vila...
}
}
Finally, there is the director which is in charge for constructing the house.
public class HouseDirector
{
public void ConstractHouse(AbstractHouseBuilder builder)
{
if (builder == null)
throw new ArgumentNullException();
builder.BuildFloor();
builder.BuildRoof();
builder.BuildWindows();
}
}
You can download the code here.
Summary
We use the Builder design pattern to design an object that can serve as an object factory, but separate the creation of the object from the representation of the object.
- The Builder design pattern is used when the creation of complex objects needs to be independent of its representation.
- The creation process must allow different representations or the object that is constructed
Whenever I consider using it I ask myself the following questions:
- Do my classes share common elements?
- Do I want or need to build objects in a step wise manner?
- Will I need to build different complex classes comprised of common elements?
תגים:Architecture, Design Patterns