Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Builder.
- Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Director.
- Right-click on Director and select Add > Operation from the popup menu.
- Name it as Construct().
- Move the cursor over Director, make use of resource icon Aggregation > Class to create an associated class Builder.
- Set the Builder abstract by right clicking on Builder and selecting Model Element Properties > Abstract from the popup menu.
- Create an operation BuildPart() in Builder.
- Set BuildPart() abstract by right clicking on it and selecting Model Element Properties > Abstract from the popup menu.
- Move the cursor over Builder, make use of resource icon Generalization > Class to create a subclass ConcreteBuilder.
- Inherit operations from Builder by right clicking on ConcreteBuilder and selecting Related Elements > Realize all Interfaces from the popup menu.
- Add an operation GetResult() to ConcreteBuilder. Up to now, the diagram should look like this:
- Move the cursor over ConcreteBuilder, make use of resource icon Dependency > Class to create a depending class Product.
- In practice, there can be multiple concrete builder classes. To represent this, we 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, click > to assign it to the Selected Stereotype list. Click OK to confirm.
- In practice, there may be multiple to operations in the Builder class for creating parts. To represent this, we need to assign a special stereotype to the Builder class. Right-click on Builder and select Stereotypes > Stereotypes... from the popup menu.
- Assign 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 name. The diagram should now become:
Defining 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 Builder. Keep the file name as is. Click OK to proceed.
Applying Design Pattern on Class Diagram
In this section, we are going to apply the builder pattern in modeling a report printing system that offers printing PDF, RTF, and HTML report one after the other, with same content in them.
- Create a new project Report Printer.
- Create a class diagram 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 at 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 header, content and footer. Press the + button, and select New Operation... to add more operations.
- In the Operation Specification, name the operation printContent. Check Abstract at the bottom of dialog box. Click OK to confirm.
- Repeat steps 10 and 11 to create the printFooter method.
- Now, clone the ConcreteBuilder class to make it have 3 subclasses as printers for PDF, RTF and HTML reports. Select ConcreteBuilder in preview pane first.
- Click on 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 on diagram.
- Tidy up the diagram to make it looks like below:
- Inherits the operations from the Printer class to the concrete printer classes. Right-click on PDFPrinter class and select Related Elements > Realize all Interfaces from the popup menu.
- Do the same for classes HTMLPrinter and RTFPrinter.
- Rename the role Builder to Printer. The result should look like this: