Introduction

Builder pattern Separate the construction of a complex object from its representation so that the same construction process can create different representations. It is used to construct a complex object step by step and the final step will return the object.

Intent

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Parse a complex representation, create one of several targets.

Implementation

The Builder design pattern uses the Factory pattern to decide which concrete class to initiate in order to build the desired type of object.

The participants classes in this pattern are:

  • Builder : This class specifies an abstract interface for creating Product object. This abstract base class defines all of the steps that must be taken in order to correctly create a product. Each step is generally abstract as the actual functionality of the builder is carried out in the concrete subclasses.
  • ConcreteBuilder : It constructs and puts together parts of the product by implementing the Builder interface. It defines and keeps track of the representation it creates and provides an interface for saving the product.
  • Director : It constructs the complex object using the Builder interface. The director class controls the algorithm that generates the final product object.
  • Product : It represents the complex object that is being built.

The client, that may be either another object or the actual client that calls the main() method of the application, initiates the Builder and Director class. The Builder represents the complex object that needs to be built in terms of simpler objects and types. The constructor in the Director class receives a Builder object as a parameter from the Client and is responsible for calling the appropriate methods of the Builder class. In order to provide the Client with an interface for all concrete Builders, the Builder class should be an abstract one. This way you can add new types of complex objects by only defining the structure and reusing the logic for the actual construction process. The Client is the only one that needs to know about the new types, the Director needing to know which methods of the Builder to call.

Example

// Abstract Builder
class abstract class TextConverter{
  abstract void convertCharacter(char c);
  abstract void convertParagraph();
}

// Product
class ASCIIText{
  public void append(char c){ 
    // Code
  }
}

// Concrete Builder
class ASCIIConverter extends TextConverter{
  // Resulting product
  ASCIIText asciiTextObj;

  // Converts a character to target representation
  object void convertCharacter(char c){
    // Code
  }

  void convertParagraph(){}
  
  ASCIIText getResult(){
    return asciiTextObj;
  }
}

// Abstracts the document object
class Document{
  static int value;
  char token;
  public char getNextToken(){
    //Get the next token
    return token;
  }
}

// Director
class RTFReader{

  TextConverter builder;

  RTFReader(TextConverter obj){
      builder=obj;
  }
    
  void parseRTF(Document doc){
    while ((t=doc.getNextToken())!= EOF){
      switch (t){
        case CHAR: builder.convertCharacter(t);
        case PARA: builder.convertParagraph();
      }
    }
  }
}

// Client
public class Client{
    
  void createASCIIText(Document doc){
    ASCIIConverter asciiBuilder = new ASCIIConverter();
    RTFReader rtfReader         = new RTFReader(asciiBuilder);
    rtfReader.parseRTF(doc);
    
    ASCIIText asciiText = asciiBuilder.getResult();
  }
  
  public static void main(String args[]){
    Client client     = new Client();
    Document doc      = new Document();
    client.createASCIIText(doc);
  }
}

Relations with Other Patterns

  • Many designs start by using Factory Method (less complicated and more customizable via subclasses) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, but more complicated).
  • Builder focuses on constructing complex objects step by step. Abstract Factory specializes in creating families of related objects.
  • Abstract Factory returns the product immediately, whereas Builder lets you run some additional construction steps before fetching the product.
  • You can use Builder when creating complex Composite trees because you can program its construction steps to work recursively.