creativity in code.

creativity is that spark of electricity before an explosion.
it allows us, in a single instance to explore all possibilities without hesitation.

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



Other pages of interest:
[+] Programming - Design patterns in Software
[+] Case studies - Software in business
[+] Human Computer Interaction
[+] RAD (Rapid Application Development)
[+] IBM has moved further into the compliance market with the acquisition of Texas based BuildForge, I..
[+] WebLogic man goes full circle with SpringSource
[+] NET application server fundamentals for the enterprise
[+] .NET Enterprise provides a truly robust and scalable platform to build business applications on I..
[+] The Future of Javascript - The Creator ponders
[+] Java and .NET both bring something to the Enterprise arena
[+] Microsoft adds workflow to cloud-based SOA platform
[+] Privacy Policy
[+] Programming - Design patterns in Software


Comments(0)

add comment Add Comment

Add your comment

Email address:
Your name:
Do you want your email to be visible?: