Builder Pattern Tutorial

This tutorial is aimed to guide the definition and application of Gang of Four (GoF) builder design pattern. By reading this tutorial, you will know how to develop a model for the builder pattern, and how to apply it in practice.

September 28, 2009
Views: 50,000
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Builder.
    new class diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class. Name it Director.
    create director class
  4. Right-click on Director and select Add > Operation from the popup menu.
    new oper
  5. Name it Construct().
    new construct oper
  6. Move the cursor over Director and use the resource icon Aggregation > Class to create an associated class named Builder.
    create builder class
  7. Set the Builder class as abstract by right-clicking on it and selecting Model Element Properties > Abstract from the popup menu.
    set builder abstract
  8. Create an operation named BuildPart() in the Builder class.
    add oper to builder
  9. Set BuildPart() as abstract by right-clicking on it and selecting Model Element Properties > Abstract from the popup menu.
    set oper abstract
  10. Move the cursor over Builder and use the resource icon Generalization > Class to create a subclass named ConcreteBuilder.
    create concrete builder
  11. Inherit operations from Builder by right-clicking on ConcreteBuilder and selecting Related Elements > Realize all Interfaces from the popup menu.
    realize
  12. Add an operation named GetResult() to ConcreteBuilder. Up to now, the diagram should look like this:
    get result added to concrete builder
  13. Move the cursor over ConcreteBuilder and use the resource icon Dependency > Class to create a depending class named Product.
    created product
  14. In practice, there can be multiple concrete builder classes. To represent this, you need to assign a special stereotype to the ConcreteBuilder class. Right-click on ConcreteBuilder and select Stereotypes > Stereotypes... from the popup menu.
    stereotype concrete builder
  15. Select PTN Cloneable and click > to assign it to the Selected Stereotype list. Click OK to confirm.
    stereotype assigned
  16. In practice, there may be multiple operations in the Builder class for creating parts. To represent this, you need to assign a special stereotype to the Builder class. Right-click on Builder and select Stereotypes > Stereotypes... from the popup menu.
    stereotype builder
  17. Assign the stereotype PTN Members Creatable to the class. Click OK to confirm.
    stereotype assigned
  18. For the association end of the association between the Director and Builder classes, there is a role named Builder. Double-click on the Director end and enter Builder as the name. The diagram should now look like this:
    class diagram done

Defining a Pattern

  1. Select all classes on the class diagram.
    select all classes
  2. Right-click on the selection and select Define Design Pattern... from the popup menu.
    define design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name as Builder. Keep the file name as is and click OK to proceed.
    define dp

Applying a Design Pattern to a Class Diagram

In this section, we will apply the builder pattern to model a report printing system that can print PDF, RTF, and HTML reports one after another, with the same content in each.

  1. Create a new project named Report Printer.
  2. Create a class diagram named Report Printer.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
    apply design pattern
  4. In the Design Pattern dialog box, select Builder from the list of patterns.
    select builder pattern
  5. Click on Director in the preview.
    select director
  6. Rename Director to ReportPrinter in the bottom pane.
    name director
  7. Rename the operation Construct to printReport.
    name oper
  8. Rename the class Builder to Printer.
    rename builder
  9. Rename the operation BuildPart to PrintHeader.
    print header
  10. We need three operations in the Printer class for printing the header, content, and footer. Click the + button and select New Operation... to add more operations.
    new oper
  11. In the Operation Specification, name the operation "printContent." Check the Abstract box at the bottom of the dialog box and click OK to confirm.
    name oper
  12. Repeat steps 10 and 11 to create the printFooter method.
    opers added
  13. Now, let's clone the ConcreteBuilder class to create three subclasses as printers for PDF, RTF, and HTML reports. Select ConcreteBuilder in the preview pane first.
    select concrete builder
  14. Click the + button and select Clone.
    select clone
  15. Select 2 as the number of classes to clone.
    select clone count
  16. Rename the classes and operations as shown below:
    rename class and opers
  17. Click OK to confirm the changes and apply the pattern to the diagram.
  18. Tidy up the diagram to make it look like the one below:
    diagram neat
  19. Inherit the operations from the Printer class to the concrete printer classes. Right-click on the PDFPrinter class and select Related Elements > Realize all Interfaces from the popup menu.
    inherit opers
  20. Do the same for the HTMLPrinter and RTFPrinter classes.
  21. Rename the role Builder to Printer. The result should look like this:
    result