DESIGN PATTERNS

The Builder Pattern
by Faisal Jawaid, 08/25/02

     The 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.  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 the same for a specified model of car.

     In Builder, the client instructs the builder class 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 the Builder Pattern

Class Diagram Showing the Builder Pattern

Practical Implementation of the Builder Pattern

Screen Shots
Screen Shots

 

The Object Constructed According to User Selection
The Object Constructed According to User Selection

C# Implementation

IBuilder Interface

using System;
using System.Windows.forms; 

namespace BuilderPattern
{
      /// <summary>
      /// Summary description for IBuilder.
      /// </summary>
      public interface IBuilder
      {           
            void ManufactureCar();
      }
}

Classes that extends from IBuilder interface

1)    SuzukiMehran Class

using System;
using System.Windows.forms; 

namespace
BuilderPattern
{
      /// <summary>
      /// Summary description for SuzukiMehran.
      /// </summary>
      public class SuzukiMehran:IBuilder
      {
            public void ManufactureCar()
            {
                   MessageBox.Show("Suzuki Mehran Model 2002 "
                                  +"Color Balck "
                                  + "Air Conditioned " ); 
            }
      }
}

     2)   SuzukiKhyber Class

using System;
using System.Windows.forms;

namespace BuilderPattern
{
  
/// <summary>
  /// Summary description for SuzukiKhyber.
  /// </summary>
  public class SuzukiKhyber:IBuilder
  {
      public SuzukiKhyber()
      {
      }

      public void ManufactureCar()
      {
           MessageBox.Show("Suzuki Khyber Model 2002 Standard "
                  +"Color Red "
                  +"Air Conditioned "
                  +"Alloy Rim "); 
      }
  }
}

3) Director class

using System;

namespace BuilderPattern
{
      /// <summary>
      /// Summary description for Director.
      /// </summary>
      public class Director
      {
            public void ConstructCar(IBuilder build)
            {
                  build.ManufactureCar(); 
            }
      }
}

   4) 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 declares one function, ManufactureCar.  Two classes SuzukiMehran and SuzukiKhyber implement the interface IBuilder and the interface function ManufactureCar.  Now we have a Director class with a method, ConstructCar, which accepts an argument of type IBuilder. The ConstructCar method then calls the ManufactureCar method of some class, depending upon the reference type variable, build, which is the type of object it's child class is pointing to. The client class just decides what type of object to create, either SuzukiMehran or SuzukiKhyber, and then passes this object as an argument to the Director class method, ManufactureCar.

Faisal Jawaid is a software engineer from Pakistan, Working in a professional enviroment for a year, currently working with C# and VB.NET.  He also has experience of programming in Java, VB6, and technologies such as Servlets and JSP.