Mediator Pattern Tutorial
This tutorial is designed to guide you through the definition and application of the Gang of Four (GoF) mediator design pattern. By following this tutorial, you will learn how to create a UML class diagram for the mediator pattern and save it as a design pattern file that can be used in the future.
What is Mediator Design Pattern?
Mediator Design Pattern is a behavioral design pattern that promotes loose coupling among the components of a system by defining a mediator object that encapsulates the communication between these components. This pattern helps to reduce the direct dependencies between the components, making them easier to maintain and modify.
In Mediator Design Pattern, a mediator object acts as a communication hub, receiving messages from the components and sending them to the appropriate recipients. This allows the components to communicate with each other indirectly, without having to know about each other’s existence.
The Mediator Design Pattern is particularly useful in large-scale systems, where the number of components and their interactions can quickly become complex. By introducing a mediator object, the system can be broken down into smaller, more manageable pieces, with the mediator handling the communication between them.
Overall, the Mediator Design Pattern can help to improve the modularity, extensibility, and maintainability of a system, by promoting loose coupling and reducing direct dependencies between the components.
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:
Saving the 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: