Modeling Design Pattern with Class Diagram
- Create a new project Design Pattern.
- Create a class diagram Prototype.
- Select Class from diagram toolbar. Click on diagram to create a class. Name it as Client.
- Right click on the Client class, and select Add > Operation from the popup menu.
- Name the operation Operation().
- Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class Prototype.
- Right click on Prototype, and select Model Element Properties > Abstract to set it as abstract.
- Add an operation Clone() to Prototype. Make it return Prototype.
- Right click on Clone(), and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the Prototype class, and drag out Generalization > Class to create a subclass ConcretePrototype.
- Make ConcretePrototype inherit the abstract operations provided from Prototype by right clicking on ConcretePrototype, and selecting Related Elements > Realize all Interfaces from the popup menu.
- In practice, there may be multiple ConcretePrototype classes. To represent this, stereotype the ConcretePrototype class as PTN Cloneable. Right click on ConcretePrototype class and select Stereotypes > Stereotypes... from the popup menu.
In the Stereotypes tab of class specification, select PTN Cloneable and click > to assign it to the class. Click OK to confirm.
The diagram should look like this:
- 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 Prototype. Keep the file name as it. Click OK to proceed.
Applying Design Pattern on Class Diagram
In this section, we will try to make use of the prototype pattern to model a part of diagram editor.
- Create a new project My Diagram Tool.
- 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 Prototype from the list of patterns.
- Click on Client in the overview.
- Rename it to EditTool at the bottom pane.
- Rename Operation to Duplicate.
- Select Prototype in overview.
- Rename Prototype to Shape at the bottom pane, and rename the operation Clone to Duplicate.
- Select ConcretePrototype in overview.
- Rename ConcretePrototype to OvalShape at the bottom pane, and rename the operation Clone to duplicate.
- We need to have two more concrete prototype classes for square and triangle. Keep ConcretePrototype selecting, click the + button, and select Clone... from the popup menu.
- Enter 2 to be the number of classes to clone.
Rename ConcretePrototype2 and ConcretePrototype3 to Square and Triangle respectively.
Rename the two Clone operations to duplicate.
- Click OK to confirm editing and apply the pattern to diagram.
- Tidy up the diagram. It should become: