Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Iterator.
- Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Aggregate.
- Right-click on Aggregate, and select Model Element Properties > Abstract to set it as abstract.
- Right-click on the Aggregate class, and select Add > Operation from the popup menu.
- Name the operation CreateIterator(), and make it return Iterator.
- Right-click on CreateIterator() : Iterator, and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the Aggregate class, and drag out Generalization > Class to create a subclass ConcreteAggregate.
- ConcreteAggregate will inherit the operations from Aggregate. Select ConcreteAggregate, right-click on them and select Related Elements > Realize all Interfaces from the popup menu.
- Create a class Client near Aggregate.
- Move the mouse cursor over the Client class, and drag out Association > Class to Aggregate.
- Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class Iterator.
- Right-click on Iterator, and select Model Element Properties > Abstract to set it as abstract.
- Right-click on the Iterator class, and select Add > Operation from the popup menu to add operations: First(), Next(), IsDone(), CurrentItem().
- Select all operations in Iterator. Right-click and select Model Element Properties > Abstract to set it as abstract.
- ConcreteIterator will inherit the operations from Iterator. Select ConcreteIterator, right-click on them and select Related Elements > Realize all Interfaces from the popup menu.
- Move the mouse cursor over the ConcreteAggregate class, and drag out Dependency > Class to class ConcreteIterator.
- Move the mouse cursor over the ConcreteIterator class, and drag out Association > Class to class ConcreteAggregate.
- In practice, there may be multiple ConcreteAggregate classes. To represent this, stereotype the ConcreteAggregate class as PTN Cloneable. Right-click on ConcreteAggregate 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.
-
There may be multiple ConcreteIterator classes. Right-click on ConcreteIterator class and select Stereotypes > PTN Cloneable from the popup menu.
The diagram 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 Iterator. 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 iterator pattern to model the use of iterator in a diagram editor.
- 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 Iterator from the list of patterns.
- Click on Aggregate in the overview.
- Rename Aggregate to AbstractList at the bottom pane.
- Select ConcreteAggregate in overview, and rename it to ShapeList at the bottom pane.
- Select ConcreteIterator in overview, and rename it to ShapeListIterator at the bottom pane.
- Click OK to apply the pattern to diagram.
- Tidy up the diagram. Here is the result: