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

  1. Create a new project Design Patterns.
  2. Create a class diagram Mediator.
    new diagram
  3. Select Class from diagram toolbar. Click on diagram to create a class. Name it as Colleague.
    create colleague
  4. Right-click on Colleague, and select Model Element Properties > Abstract to set it as abstract.
    set colleague abstract
  5. Move the mouse cursor over the Colleague class, and drag out Generalization > Class to create a subclass ConcreteColleague.
    create concrete colleague
  6. Move the mouse cursor over the Colleague class, and drag out Association > Class to create an associated class Mediator.
    create mediator
  7. Right-click on Colleague, and select Model Element Properties > Abstract to set it as abstract.
  8. Move the mouse cursor over the Mediator class, and drag out Generalization > Class to create a subclass ConcreteMediator.
    create concrete mediator
  9. Associate ConcreteMediator and ConcreteColleague.
    associate concrete mediator colleague
  10. 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.
    stereotype concrete colleague
  11. In the Stereotypes tab of class specification, select PTN Cloneable and click > to assign it to the class. Click OK to confirm.
    set ptn cloneable
    The diagram should become:
    pattern modeled

Saving the Defining Pattern

  1. Select all classes on the class diagram.
    select all classes
  2. Right-click on the selection and select Define Design Pattern… from the popup menu.
    define design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name Mediator. Keep the file name as is. Click OK to proceed.
    name pattern

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.

  1. Create a new project File Explorer.
  2. Create a class diagram Domain Model.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern… from the popup menu.
    apply design pattern
  4. In the Design Pattern dialog box, select Mediator from the list of patterns.
    select mediator in dialog box
  5. A the bottom of pane, rename ColleagueConcreteColleagueConcreteMediatorMediator to WidgetFileListFileExplorerDirectorExplorerDirector.
    rename classes
  6. We need one more concrete colleague – FolderList. Click on the + button next to ConcreteColleauge and select Clone… from popup menu.
    clone
  7. Enter 1 as the number of classes to clone.
    clone count
  8. Rename ConcreteColleague2 to FolderList.
    rename concrete colleague 2
  9. Click OK to confirm editing and apply the pattern to diagram.
  10. Tidy up the diagram. It should become:
    result

Resources

  1. Design Patterns.vpp
  2. Mediator.pat

Related Links

Leave a Comment

Your email address will not be published.