Sunday, July 18, 2010

The builder pattern

Definition
Separate the construction of a complex object from its representation so that the same construction process can create different representations.

The Class diagram





The implementation

The computer builder is an abstract class (builder). The client use this abstract class to build the product.

public abstract class ComputerBuilder
{
protected Computer computer;
public Computer Computer
{
get { return computer; }
}

public abstract void BuildDisplay();
public abstract void BuildMotherBoard();
public abstract void BuildKeybord();
public abstract void BuildHardDisk();
public abstract void BuildPowerUnit();
}

The LaptopBuilder and DesktopBuilder are concrete builders and they create the real product parts and store them in the part composite structure.

public class LaptopBuilder : ComputerBuilder
{
public LaptopBuilder()
{
computer = new Computer("Laptop Computer");
}

public override void BuildDisplay()
{
computer["display"] = "LCD integrated Display";
}

public override void BuildMotherBoard()
{
computer["mother_board"] = "Motherbord with centrino processor";
}

public override void BuildKeybord()
{
computer["keybord"] = "Integrated Keybord";
}

public override void BuildHardDisk()
{
computer["hard_disk"] = "Integrated Hard Disk";
}

public override void BuildPowerUnit()
{
computer["power_unit"] = "Integrated power unit";
}
}



public class DesktopBuilder : ComputerBuilder
{
public DesktopBuilder()
{
computer = new Computer("Desktop Computer");
}
public override void BuildDisplay()
{
computer["display"] = "LCD separated Display";
}

public override void BuildMotherBoard()
{
computer["mother_board"] = "Motherbord with processor";
}

public override void BuildKeybord()
{
computer["keybord"] = "Separate Keybord";
}

public override void BuildHardDisk()
{
computer["hard_disk"] = "Hard Disk";
}

public override void BuildPowerUnit()
{
computer["power_unit"] = "Power unit";
}
}
This is the product and concrete builder responsible for building this by building parts.

public class Computer
{
private string _computerType;
private Dictionary _parts = new Dictionary();
public string this[string key]
{
get { return _parts[key]; }
set { _parts[key] = value; }
}
public Computer(string computerType)
{
_computerType = computerType;
}

public void Show()
{
Console.WriteLine("\n---------------------------");
Console.WriteLine("Computer Type: {0}", _computerType);
Console.WriteLine(" Display : {0}", _parts["display"]);
Console.WriteLine(" Motherbord : {0}", _parts["mother_board"]);
Console.WriteLine(" Keybord: {0}", _parts["keybord"]);
Console.WriteLine(" Hard Disk : {0}", _parts["hard_disk"]);
Console.WriteLine(" Power unit : {0}", _parts["power_unit"]);
}
}

The Shop (Director) construct the object using the builder interface.


public class Shop
{
public void Construct(ComputerBuilder computerBuilder)
{
computerBuilder.BuildDisplay();
computerBuilder.BuildHardDisk();
computerBuilder.BuildKeybord();
computerBuilder.BuildMotherBoard();
computerBuilder.BuildPowerUnit();
}
}



class Program
{
static void Main(string[] args)
{
ComputerBuilder computerBuilder;
Shop shop = new Shop();
computerBuilder = new LaptopBuilder();
shop.Construct(computerBuilder);
computerBuilder.Computer.Show();
Console.ReadLine();
}
}


The pattern can apply:
  • If the algorithm of creating complex objects should separate parts making how they are assembled.
  • If you need control over the construction

No comments: