You are on page 1of 5

1

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

Participants Builder (VehicleBuilder) o specifies an abstract interface for creating parts of a Product object ConcreteBuilder (MotorCycleBuilder, CarBuilder, ScooterBuilder) o provides implementation for Builder o constructs and assemble parts to build the products Director (Shop) o constructs an object using the Builder interface Product (Vehicle) o the complex object under construction

Director Class The Director class is responsible to manage the correct sequence of object creation. It receives a Concrete Builder as parameter and execute on it the necessary operations Example:

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;

3
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.

Practical Implementation of Builder Pattern

Screen Shots

The Object Constructed according to user Selection

Implementation of Builder Design Pattern in ADO.NET 2.0 DbConnectionStringBuilder class serves as a base class for strongly typed connection string builders. The DbConnectionStringBuilder class provides the base class from which the strongly typed connection string builders (SqlConnectionStringBuilder, OleDbConnectionStringBuilder, and so on) derive. The connection string builders let developers programmatically create syntactically correct connection strings, and parse and rebuild existing connection strings. The DBConnectionStringBuilder sits in the System.Data.Common namespace to provide a datastore agnostic interface. You would work with it like this:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "(local)"; builder.InitialCatalog = "Product"; builder.IntegratedSecurity = true; SqlConnection connection = new SqlConnection(builder.ToString()); // ...

As you can tell from the above code, you are building a connection string in a very object-oriented way step-by-step, and you don't need any intimate knowledge about connection string formats. Once you have set all of the properties particular to your environment in any order, it is the call to the ToString() method that outputs an appropriate connection string for you.

Difference between Abstract Factory and Builder design pattern

5
The factory pattern defers the choice of what concrete type of object to make until run time . E.g. going to a restaurant to order the special of the day. The waiter is the interface to the factory that takes the abstractor generic message "Get me the special of the day!" and returns the concrete product (i.e. Hawaiian or Spicy pizza) The builder pattern encapsulates the logic of how to put together a complex object so that the client just requests a configuration and the builder directs the logic of building it. E.g The main contractor (builder) in building a house knows, given a floor plan, how to execute the sequence of operations (i.e. by delegating to subcontractors) needed to build the complex object. If that logic was not encapsulated in a builder, then the buyers would have to organize the subcontracting themselves ("Dear, shouldn't we have asked for the foundation to be laid before the roofers showed up?") The factory is concerned with what is made, the builder with how it is made. Design patterns points out that Abstract factory is similar to builder in that it too may construct complex objects. The primary difference is that the Builder pattern focuses on constructing a complex object step by step. Abstract factory's emphasis is on families of product objects (either simple or complex). Builder returns the product as the final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.

You might also like