Jump to Menu

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.

Compatible edition(s): Enterprise, Professional, Standard

  • October 21, 2009
  • Views: 28,883
  • PDF

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

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 Colleague, ConcreteColleague, ConcreteMediator, Mediator to Widget, FileList, FileExplorerDirector, ExplorerDirector.
    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



创造美好 共同成长

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.

OK