Programming - Builder Design Pattern


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


Classes and/or objects participating in this example are:


  • Builder
    • specifies an abstract interface for creating parts of a Product object
  • ConcreteBuilder
    • constructs and assembles parts of the product by implementing the Builder interface
    • defines and keeps track of the representation it creates
    • provides an interface for retrieving the product
  • Director
    and
    • constructs an object using the Builder interface
  • Product
    and
    • represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled
    • includes classes that define the constituent parts, including interfaces for assembling the parts into the final result
// Builder pattern -- Structural example

using
System;
using
System.Collections;

namespace DoFactory.GangOfFour.Builder.Structural
{
// MainApp test application

public class MainApp
{
public static void Main()
{
// Create director and builders
Director director = new Director();

Builder b1 = new ConcreteBuilder1();
Builder b2 = new ConcreteBuilder2();

// Construct two products
director.Construct(b1);
Product p1 = b1.GetResult();
p1.Show();

director.Construct(b2);
Product p2 = b2.GetResult();
p2.Show();

// Wait for user
Console.Read();
}
}

// "Director"

class Director
{
// Builder uses a complex series of steps
public void Construct(Builder builder)
{
builder.BuildPartA();
builder.BuildPartB();
}
}

// "Builder"

abstract class Builder
{
public abstract void BuildPartA();
public abstract void BuildPartB();
public abstract Product GetResult();
}

// "ConcreteBuilder1"

class ConcreteBuilder1 : Builder
{
private Product product = new Product();

public override void BuildPartA()
{
product.Add("PartA");
}

public override void BuildPartB()
{
product.Add("PartB");
}

public override Product GetResult()
{
return product;
}
}

// "ConcreteBuilder2"

class ConcreteBuilder2 : Builder
{
private Product product = new Product();

public override void BuildPartA()
{
product.Add("PartX");
}

public override void BuildPartB()
{
product.Add("PartY");
}

public override Product GetResult()
{
return product;
}
}

// "Product"

class Product
{
ArrayList parts = new ArrayList();

public void Add(string part)
{
parts.Add(part);
}

public void Show()
{
Console.WriteLine("\nProduct Parts -------");
foreach (string part in parts)
Console.WriteLine(part);
}
}
}
Output to be expected:
and
Product Parts -------
PartA
PartB

Product Parts -------
PartX
PartY


Home | Privacy Policy | Site Map | Links | Company | Contact Us | Arena           

We provide cutting edge software development, consultancy and web design services            

There are currently 648 user(s) with 40885 hits.