Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Bridge.
- Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Abstraction.
- Right-click on Abstraction, and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the Abstraction class, and drag out Aggregation > Class to create an associated class Implementor.
- Right-click on Implementor, and select Model Element Properties > Abstract to set it as abstract.
- Right-click on the Abstract class, and select Add > Operation from the popup menu.
- Name the operation Operation().
- Right-click on the Implementor class, and select Add > Operation from the popup menu. Name the operation OperationImpl().
- Right-click on Implementor, and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the Abstraction class, and drag out Generalization > Class to create a subclass RefinedAbstraction.
- Repeat the previous step to create a subclass ConcreteImplementor from Implementor.
- ConcreteImplementor will inherits the operations from Implementor. Right-click on ConcreteImplementor and select Related Elements > Realize all Interfaces from the popup menu.
- In practice, there may be multiple refined abstractions and/or concrete implementors. To represent this, stereotype the class RefinedAbstraction and ConcreteImplementor as PTN Cloneable. Right-click on Abstraction and select Stereotypes > Stereotypes... from the popup menu.
- In the Stereotypes tab of the Class Specification dialog box, select PTN Cloneable and click > to assign it to RefinedAbstraction class. Click OK to confirm.
- Repeat steps 14 and 15 on concreteImplementor.
- In practice, there may be multiple operations and/or operationImpls. To represent this, stereotype the class Abstraction and Implementor as PTN Members Creatable. Repeat steps 14 and 15 to stereotype Abstraction and Implementor as PTN Members Creatable.
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 Bridge. 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 bridge pattern to model a report generator for various report types.
- Create a new project Diagram Editor.
- Create a class diagram Domain Model.
- Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
- In the Design Pattern dialog box, select Bridge from the list of patterns.
- Click on Abstraction in the overview.
- Rename Abstraction to ReportGenerator and operation Operation to generate at the bottom pane.
- Select RefinedAbstraction in the overview pane.
- Rename RefinedAbstraction to HTMLReportGenerator.
- Besides HTML report generator, we need also a Plain Text report generator. Click on the + button at the bottom pane, beside Abstraction, and select Clone...
- Enter 1 to be the number of classes to clone. Click OK to confirm.
- Rename RefinedAbstraction2, the cloned class, to PlainTextReportGenerator.
- Select Implementor in the overview pane.
- Rename Implementor to ReportGeneratorImpl, and OperationImpl to generateTOC.
- We need 2 more operations for generating content and appendix. Click on the + button and select New Operation... from the popup meun.
- In the Operation Specification dialog box, name the operation generateContent. Check Abstract at the bottom of the dialog box.
- Repeat the previous steps to create another abstract operation generateAppendix.
- Select ConcreteImplementor in overview. Rename ConcreteImplementor to SimpleReportGeneratorImpl, and operation OperationImpl to generateTOC.
- Similar to RefinedAbstraction, here we need to have another concrete implementor for generating complex report. Click on the + button and select Clone... from the popup menu.
- Enter 1 to be the number of classes to clone. Click OK to confirm.
- Rename ConcreteImplmentor2, the cloned class, to ComplexReportGeneratorImpl, and operation OperationImpl to generateTOC.
- Click OK to confirm. Here is the diagram formed:
- We want HTMLReportGenerator and PlainTextReportGenerator to implement their own way of generating repot. Select the generate operation is ReportGenerator.
- Press the Ctrl key, and drag to HTMLReportGenerator. Replease the mouse button afterwards.
- Repeat the previous steps to create the generate method in PlainTextReportGenerator. Here is the completed diagram: