By Faisal Jawaid
Builder Pattern
Builder Pattern is quite confusing. There is a very subtle difference between Builder Pattern and Factory Pattern. I am not discussing Factory Pattern in this article but will soon write about Factory Pattern as well.
In Builder Pattern the user is given the choice to create the type of object he wants but the construction process is the same.
We consider an example suppose you went to buy a car. First you will choose the model of the car and then it's color and you consider it's price that whether it's feasible for you or not and if you want some extra luxuries in the car you will note them down as well and in the end you will place the order. Now you are isolated with the manufacturing process. You are not bothered by the manufacturing process it's the duty of the company because they have the assembly plant, which is programmed to manufacture cars of the model you choose and the manufacturing process is same for a specified model of car.
In Builder the client instructs the builder class on how to create the object and then asks it for the result. How the class is put together is up to the Builder class

Class Diagram Showing Builder Pattern
Practical Implementation of Builder Pattern

Screen Shots

The Object Constructed according to user Selection
C# Implementation
IBuilder Interface
using System;
using System.Windows.Forms;
namespace BuilderPattern
{
///
/// Summary description for IBuilder.
///
public interface IBuilder
{
void MnaufactureCar();
}
}
Classes that extends from IBuilder interface
1) SuzukiMehran Class
using System;
using System.Windows.Forms;
namespace BuilderPattern
{
///
/// Summary description for SuzukiMehran.
///
public class SuzukiMehran:IBuilder
{
public void MnaufactureCar()
{
MessageBox.Show("Suzuki Mehran Model 2002 "
+"Color Balck "
+ "Air Conditioned " );
}
}
}
2) SuzukiKhyber Class
using System;
using System.Windows.Forms;
namespace BuilderPattern
{
///
/// Summary description for SuzukiKhyber.
///
public class SuzukiKhyber:IBuilder
{
public SuzukiKhyber()
{
}
public void MnaufactureCar()
{
MessageBox.Show("Suzuki Khyber Model 2002 Standard "
+"Color Red "
+"Air Conditioned "
+"Alloy Rim ");
}
}
}
Director class
using System;
namespace BuilderPattern
{
///
/// Summary description for Director.
///
public class Director
{
public void ConstructCar(IBuilder build)
{
build.MnaufactureCar();
}
}
}
Client Class
private void button1_Click(object sender, System.EventArgs e)
{
if(cmbChooseCar.Text=="Suzuki Mehran")
{
Director car=new Director();
IBuilder build=new SuzukiMehran();
car.ConstructCar(build);
}
if(cmbChooseCar.Text=="Suzuki Khyber")
{
Director car=new Director();
IBuilder build=new SuzukiKhyber();
car.ConstructCar(build);
}
}
Description of program
The above program contains an interface IBuilder which declare one function ManufactureCar. Two classes SuzukiMehran and SuzukiKhyber implement this interface IBuilder and also implements the interface function ManufactureCar. Now we have a Director class which contains a method Constructcar which accepts an argument of type IBuilder and then call Manufacturecar method of any one Class depending upon the reference type variable build that which type of object of its child class it is pointing. The Client class just decides what type of object to create either SuzukiMehran or SuzukiKhyber and then pass this object as an argument to director method ManufactureCar.