Mediator Pattern Tutorial
This tutorial is aimed to guide the definition and application of Gang of Four (GoF) mediator design pattern. By reading this tutorial, you will know how to develop a model for the mediator pattern, and how to apply it in practice.
Modeling a Design Pattern with a Class Diagram
- Create a new project named Design Patterns.
- Create a class diagram named Mediator.

- Select Class from the diagram toolbar. Click on the diagram to create a class. Name it 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 Mediator 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 ConcreteColleague classes. To represent this, stereotype the ConcreteColleague class as PTN Cloneable. Right-click on the ConcreteColleague class and select Stereotypes > Stereotypes... from the popup menu.

-
In the Stereotypes tab of the class specification, select PTN Cloneable and click > to assign it to the class. Click OK to confirm.
The diagram should become:
Defining a 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 a Design Pattern on a 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.

- At the bottom of the pane, rename Colleague, ConcreteColleague, ConcreteMediator, Mediator to Widget, FileList, FileExplorerDirector, ExplorerDirector.

- We need one more concrete colleague - FolderList. Click on the + button next to ConcreteColleague and select Clone... from the 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 the diagram.
- Tidy up the diagram. It should become:
