Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Mediator.
- Select Class from diagram toolbar. Click on diagram to create a class. Name it as Colleague.
- Right-click on Colleague, and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the Colleague class, and drag out Generalization > Class to create a subclass ConcreteColleague.
- Move the mouse cursor over the Colleague class, and drag out Association > Class to create an associated class Mediator.
- Right-click on Colleague, and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the Mediator class, and drag out Generalization > Class to create a subclass ConcreteMediator.
- Associate ConcreteMediator and ConcreteColleague.
- In practice, there may be multiple ConcreteColleaguie classes. To represent this, stereotype the ConcreteColleague class as PTN Cloneable. Right-click on ConcreteColleague 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 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 Mediator. 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 mediator pattern to model a part of a file explorer.
- Create a new project File Explorer.
- 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 Mediator from the list of patterns.
- A the bottom of pane, rename Colleague, ConcreteColleague, ConcreteMediator, Mediator to Widget, FileList, FileExplorerDirector, ExplorerDirector.
- We need one more concrete colleague - FolderList. Click on the + button next to ConcreteColleauge and select Clone... from popup menu.
- Enter 1 as the number of classes to clone.
- Rename ConcreteColleague2 to FolderList.
- Click OK to confirm editing and apply the pattern to diagram.
- Tidy up the diagram. It should become: