Mediator Design Pattern (Behavioral)

Mediator
Design Pattern
(Behavioral)
-Seema Joshi
Intent
Define an object that encapsulates how a set of
objects interact. Mediator promotes loose
coupling by keeping objects from referring to
each other explicitly, and it lets you vary their
interaction independently.
03/04/03
CS590L Mediator Pattern
2
Motivation

Object oriented design
encourages the distribution
of behavior among objects.

Many connections between
objects in an object
structure.

Partitioning a system into
many objects enhances
reusability.

-Lots of interconnections
increases interdependency.
-Moreover it can be difficult
to change the systems'
behavior in any significant
way, since behavior is
distributed among many
objects.
03/04/03
CS590L Mediator Pattern
3
Applicability



When one or more objects must interact with
several different objects.
When simple object need to communicate in
complex ways.
When you want to reuse an object that frequently
interacts with other objects.
03/04/03
CS590L Mediator Pattern
4
ExampleProblem
03/04/03
CS590L Mediator Pattern
5
Example
-Solution
03/04/03
CS590L Mediator Pattern
6
Example
-Sequence Diagram
03/04/03
CS590L Mediator Pattern
7
Example
-Steps




The list box tells its director that it’s
changed.
The director gets the selection from the list
box.
The director passes the selection to the entry
field.
Director takes action.
03/04/03
CS590L Mediator Pattern
8
Example
-Structure
03/04/03
CS590L Mediator Pattern
9
Example
-Relationship



DialogDirector is an abstract class that defines
the overall behavior of a dialog.
Clients call ShowDialog()
CreateWidgets() –abstract operation for
creating widgets.
03/04/03
CS590L Mediator Pattern
10
General Structure
03/04/03
CS590L Mediator Pattern
11
Participants

Mediator
-defines an interface for communicating with Colleague
objects.

ConcreteMediator
-implements cooperative behavior by coordinating
Colleague objects.
-knows and maintains its colleagues.

Colleague classes
-each Colleague class knows its Mediator object.
-each colleague communicates with its mediator
whenever it would have otherwise communicated with
another colleague.
03/04/03
CS590L Mediator Pattern
12
Another Example
03/04/03
CS590L Mediator Pattern
13
Good Points about the Mediator




Centralized control
Less chance of miscommunication.
Simplifies object protocols
Replaces many-to-many interactions with one-to-many
interactions between mediator and colleagues, thus simple
to understand, maintain and extend.
Abstracts how objects cooperate
Splitting up the system in this way creates a better
understanding of the objects in the system, how they interact
and how they are structured .
Loose coupling between colleagues promotes Reusability
Basically, it proliferates interconnections to help eventually
reduce it.
03/04/03
CS590L Mediator Pattern
14
Bad Points about the Mediator

Overloading the Mediator
There is the potential for numerous subclasses of
the Mediator and these in turn could require
subclasses of those Mediator subclasses. It's just a
horrible cycle from this point on.

Hindrance
If there are a relatively small group of objects, it
may waste less time if they are able to speak to each
other directly rather than through a Mediator.
03/04/03
CS590L Mediator Pattern
15
Related Patterns


Facade
Observer
03/04/03
CS590L Mediator Pattern
16
??????????????
You may have questions, if you
didn’t pay attention during the
presentation.
03/04/03
CS590L Mediator Pattern
17
References


Design Patterns: Elements of Reuseable ObjectOriented Software by E. Gamma, R. Helm, R.
Johnson and J. Vlissides, Addison-Wesley, ISBN:
0-201-63361-2, Copyright 1995.
http://sern.ucalgary.ca/courses/SENG/443/W02/assi
gnments/Mediator/
03/04/03
CS590L Mediator Pattern
18
Thank You
03/04/03
CS590L Mediator Pattern
19