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.
Modeling a Design Pattern with a Class Diagram
- Create a new project named Design Patterns.
- Create a class diagram named Builder.

- Select Class from the diagram toolbar. Click on the diagram to create a class. Name it Director.

- Right-click on Director and select Add > Operation from the popup menu.

- Name it Construct().

- Move the cursor over Director and use the resource icon Aggregation > Class to create an associated class named Builder.

- Set the Builder class as abstract by right-clicking on it and selecting Model Element Properties > Abstract from the popup menu.

- Create an operation named BuildPart() in the Builder class.

- Set BuildPart() as abstract by right-clicking on it and selecting Model Element Properties > Abstract from the popup menu.

- Move the cursor over Builder and use the resource icon Generalization > Class to create a subclass named ConcreteBuilder.

- Inherit operations from Builder by right-clicking on ConcreteBuilder and selecting Related Elements > Realize all Interfaces from the popup menu.

- Add an operation named GetResult() to ConcreteBuilder. Up to now, the diagram should look like this:

- Move the cursor over ConcreteBuilder and use the resource icon Dependency > Class to create a depending class named Product.

- 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.

- Select PTN Cloneable and click > to assign it to the Selected Stereotype list. Click OK to confirm.

- 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.

- Assign the stereotype PTN Members Creatable to the class. Click OK to confirm.

- 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:

Defining a Pattern
- Select all classes on the class diagram.

- Right-click on the selection and select Define Design Pattern... from the popup menu.

- In the Define Design Pattern dialog box, specify the pattern name as Builder. Keep the file name as is and click OK to proceed.

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.
- Create a new project named Report Printer.
- Create a class diagram named Report Printer.
- Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.

- In the Design Pattern dialog box, select Builder from the list of patterns.

- Click on Director in the preview.

- Rename Director to ReportPrinter in the bottom pane.

- Rename the operation Construct to printReport.

- Rename the class Builder to Printer.

- Rename the operation BuildPart to PrintHeader.

- 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.

- In the Operation Specification, name the operation "printContent." Check the Abstract box at the bottom of the dialog box and click OK to confirm.

- Repeat steps 10 and 11 to create the printFooter method.

- 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.

- Click the + button and select Clone.

- Select 2 as the number of classes to clone.

- Rename the classes and operations as shown below:

- Click OK to confirm the changes and apply the pattern to the diagram.
- Tidy up the diagram to make it look like the one below:

- 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.

- Do the same for the HTMLPrinter and RTFPrinter classes.
- Rename the role Builder to Printer. The result should look like this:
