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.
About the author
Faisal Jawaid is a software engineer in Pakistan and working in professinal enviroment for a year. He has programming experience of Java,Vb6 Scripting Languages like javaScript, HTML,CSS etc.Currently working on DotNet technologies like C# and VbDotNet.