Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Factory Method.
- Select Class from diagram toolbar. Click on diagram to create a class. Name it as Product.
- Set the Product class abstract by right clicking on it and selecting Model Element Properties > Abstract from the popup menu.
- Move the mouse cursor over the Product class, and drag out Generalization > Class to create a subclass ConcreteProduct.
- Create a class Creator, and set it as abstract.
- Right-click on the Creator class, and select Add > Operation from the popup menu.
- Name the operation FactoryMethod(), and make it return Product.
- Right-click on FactoryMethod(), and select Model Element Properties > Abstract to set it as abstract.
- Add a non abstract operation AnOperation() to Creator.
- Move the mouse cursor over the Creator class, and drag out Generalization > Class to create a subclass ConcreteCreator.
- Make ConcreteCreator inherit the abstract operations provided from Creator by right clicking on ConcreteCreator, and selecting Related Elements > Realize all Interfaces from the popup menu.
- In practice, the FactoryMethod in ConcreteCreator is expected to return an instance of ConcreteProduct. Therefore, add a dependency between ConcreteCreator and ConcreteProduct. Move the mouse cursor over the ConcreteCreator class, and drag out Dependency > Class to ConcreteProduct. Up to now, the diagram should look like this:
- There may be more than one operations in the Creator class. To represent this, stereotype the Creator class as PTN Members Creatable. Right-click on Creator class and select Stereotypes > Stereotype from the popup menu.
- In the Stereotypes tab of class specification, select PTN Members Creatable and click > to assign it to the class. Click OK to confirm.
- The Product class should also have its own operations. Repeat step 14 and 15 to stereotype it as PTN Members Creatable.
- There may be multiple concrete products and creator. Let's repeat step 14 and 15 to stereotype ConcreteProduct and ConcreteCreator as PTN Cloneable. The diagram should look like this:
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 Factory Method. Keep the file name as is. Click OK to proceed.
Applying Design Pattern on Class Diagram
In this section, we will try to make use of the factory method pattern to model a part of a text editor.
- Create a new project Text 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 Factory Method from the list of patterns.
- Click on Product in the overview.
- Rename it to TextDocument at the bottom pane.
- Click on +, and select New Operation... from the popup menu. We shall create the operations available in the TextDocument class.
- In the Operation Specification dialog box, enter Open as operation name.
- Repeat step 7 and 8 to create operations close, save, reopen.
- Click on ConcreteProduct in the overview.
- Rename ConcreteProduct to PlainTextDocument.
- We need to process one more document type for RTF document. Keep ConcreteProduct selected and click the + button, then select Clone... in the popup menu.
- Enter 1 to be the number of classes of clone. Click OK to confirm.
- Enter RTFDocument as class name.
- Select the Creator class in overview.
- Rename Creator to TextEditor, operations FactoryMethod to createDoc, AnOperation to newDoc.
- We need more operations. Click on +, then select New Operation... from the popup menu.
- In the Operation Specification dialog box, enter loadDoc as name.
- Repeat the previous steps to create operations closeDoc, saveDoc, reopenDoc.
- Select ConcreteCreator. Rename ConcreteCreator to MyTextEditor and operation FactoryMethod to createDoc.
- Click OK to confirm editing and apply the pattern to diagram.
- Tidy up the diagram. It should become: