GOF pattern - Builder

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

Frequency of use: use_medium.gif medium
UML class diagram
builder.gif
Participants
The classes and/or objects participating in this pattern are:
  • Builder (VehicleBuilder)
    • specifies an abstract interface for creating parts of a Product object
  • ConcreteBuilder (MotorCycleBuilder, CarBuilder, ScooterBuilder)
    • 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 (Shop)
    • constructs an object using the Builder interface
  • Product (Vehicle)
    • 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
Sample code
This structural code demonstrates the Builder pattern in which complex objects are created in a step-by-step fashion. The construction process can create different object representations and provides a high level of control over the assembly of the objects.
// Builder pattern -- Structure example

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

using System;
using System.Collections;

// "Director" 
class Director {
  
// Methods
  public void Construct( Builder builder ) {
    builder.BuildPartA();
    builder.BuildPartB();
  }
}

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

// "ConcreteBuilder1" 
class ConcreteBuilder1 : Builder {
  
// Fields
  private Product product;

  
// Methods
  override public void BuildPartA() {
    product 
= new Product();
    product.Add( 
"PartA" );
  }

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

  
override public Product GetResult() {
    
return product;
  }
}
// "ConcreteBuilder2" 
class ConcreteBuilder2 : Builder {
  
// Fields
  private Product product;

  
// Methods
  override public void BuildPartA() {
    product 
= new Product();
    product.Add( 
"PartX" );
  }

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

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

// "Product" 
class Product {
  
// Fields
  ArrayList parts = new ArrayList();
  
  
// Methods
  public void Add( string part ) {
    parts.Add( part );
  }

  
public void Show() {
    Console.WriteLine( 
"\nProduct Parts -------" );
    
foreachstring part in parts )
      Console.WriteLine( part );
  }
}

/// <summary>
///   Client test
/// </summary>
public class Client {
  
public static void Main( string[] args ) { 
    
// 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();
  }
}
This real-world code demonstates the Builder pattern in which different vehicles are assembled in a step-by-step fashion. The Shop uses VehicleBuilders to construct a variety of Vehicles in a series of sequential steps.
// Builder pattern -- Real World example

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

using System;
using System.Collections;

// "Director"
class Shop {
  
// Methods
  public void Construct( VehicleBuilder vehicleBuilder ) {
    vehicleBuilder.BuildFrame();
    vehicleBuilder.BuildEngine();
    vehicleBuilder.BuildWheels();
    vehicleBuilder.BuildDoors();
  }
}

// "Builder"
abstract class VehicleBuilder {
  
// Fields
  protected Vehicle vehicle;

  
// Properties
  public Vehicle Vehicle {
    
getreturn vehicle; }
  }

  
// Methods
  abstract public void BuildFrame();
  
abstract public void BuildEngine();
  
abstract public void BuildWheels();
  
abstract public void BuildDoors();
}

// "ConcreteBuilder1"
class MotorCycleBuilder : VehicleBuilder {
  
// Methods
  override public void BuildFrame() {
    vehicle 
= new Vehicle( "MotorCycle" );
    vehicle[ 
"frame" ] = "MotorCycle Frame";
  }

  
override public void BuildEngine() {
    vehicle[ 
"engine" ] = "500 cc";
  }

  
override public void BuildWheels() {
    vehicle[ 
"wheels" ] = "2";
  }

  
override public void BuildDoors() {
    vehicle[ 
"doors" ] = "0";
  }
}

// "ConcreteBuilder2"
class CarBuilder : VehicleBuilder {
  
// Methods
  override public void BuildFrame() {
    vehicle 
= new Vehicle( "Car" );
    vehicle[ 
"frame" ] = "Car Frame";
  }

  
override public void BuildEngine() {
    vehicle[ 
"engine" ] = "2500 cc";
  }

  
override public void BuildWheels() {
    vehicle[ 
"wheels" ] = "4";
  }

  
override public void BuildDoors() {
    vehicle[ 
"doors" ] = "4";
  }
}

// "ConcreteBuilder3"
class ScooterBuilder : VehicleBuilder {
  
// Methods
  override public void BuildFrame() {
    vehicle 
= new Vehicle( "Scooter" );
    vehicle[ 
"frame" ] = "Scooter Frame";
  }

  
override public void BuildEngine() {
    vehicle[ 
"engine" ] = "none";
  }

  
override public void BuildWheels() {
    vehicle[ 
"wheels" ] = "2";
  }

  
override public void BuildDoors() {
    vehicle[ 
"doors" ] = "0";
  }
}

// "Product" 
class Vehicle {
  
// Fields
  private string type;
  
private Hashtable parts = new Hashtable();

  
// Constructors
  public Vehicle( string type ) {
    
this.type = type;
  }

  
// Indexers
  public object thisstring key ] {
    
getreturn parts[ key ]; }
    
set{ parts[ key ] = value; }
  }

  
// Methods
  public void Show() {
    Console.WriteLine( 
"\n---------------------------");
    Console.WriteLine( 
"Vehicle Type: {0}", type );
    Console.WriteLine( 
" Frame  : {0}", parts[ "frame" ] );
    Console.WriteLine( 
" Engine : {0}", parts[ "engine" ] );
    Console.WriteLine( 
" #Wheels: {0}", parts[ "wheels" ] );
    Console.WriteLine( 
" #Doors : {0}", parts[ "doors" ] );
  }
}

///
 <summary>
///   BuilderApp test
/// </summary>
public class BuilderApp {
  
public static void Main( string[] args ) {
    
// Create shop with vehicle builders
    Shop shop = new Shop();
    VehicleBuilder b1 
= new ScooterBuilder();
    VehicleBuilder b2 
= new CarBuilder();
    VehicleBuilder b3 
= new MotorCycleBuilder();

    
// Construct and display vehicles
    shop.Construct( b1 );
    b1.Vehicle.Show();

    shop.Construct( b2 );
    b2.Vehicle.Show();

    shop.Construct( b3 );
    b3.Vehicle.Show();
  }
}

posted on 2005-07-06 08:53 毒菇求Buy 阅读(221) 评论(0)  编辑 收藏 引用 所属分类: Design PatternC#

只有注册用户登录后才能发表评论。
<2005年12月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(7)

随笔分类(133)

随笔档案(111)

文章分类(65)

文章档案(53)

相册

收藏夹(30)

BLOG

Book store

Graphics Design

搜索

最新评论

阅读排行榜

评论排行榜