Mediator Pattern - Lecture Slides | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-3uq
koofers-user-3uq 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Mediator Pattern
Apr. 12, 2007
2
Example
Consider a program which has
several buttons, two list boxes
and a text entry field.
When the program starts, the
Copy and Clear buttons are
disabled.
When you select one of the
names in the left-hand list box,
it is copied into the text field for
editing, and the Copy button is
enabled.
3
Example (cont…)
When you click on Copy,
that text is added to the
right hand list box, and the
Clear button is enabled.
If you click on the Clear
button, the right hand list
box and the text field are
cleared, the list box is
deselected and the two
buttons are again disabled.
4
Relationship Diagram
The interactions between
the visual controls are
pretty complex.
Each visual object needs
to know about t wo or
more others, leading to
quite a tangled
relationship diagram.
pf3
pf4
pf5

Partial preview of the text

Download Mediator Pattern - Lecture Slides | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Mediator PatternSpring 2007

Apr. 12, 2007

Example

and a text entry field.several buttons, two list boxesConsider a program which has

disabled.Copy and Clear buttons areWhen the program starts, the

editing, and theit is copied into the text field fornames in the left-hand list box,When you select one of the

(^) Copy

(^) button is

enabled.

3

Example (cont…)

When you click on

(^) Copy,

Clear right hand list box, and the that text is added to the

(^) button is enabled.

If you click on the

(^) Clear

buttons are again disabled.deselected and the twocleared, the list box isbox and the text field are button, the right hand list

Relationship Diagram

pretty complex.the visual controls areThe interactions between

relationship diagram.quite a tangledmore others, leading toto know about two orEach visual object needs

5

Mediator

is aware of the other classes in the system.The Mediator pattern simplifies this system by being the only class that

The controls that the Mediator communicates with is a Colleague.

informed of this event.event, and the Mediator decides which other classes should beEach Colleague informs the Mediator when it has received a user

Why Mediator?

– it is the only class that knows of the other classes, andThe advantage of the Mediator is clear

classes are added.of the other classes changes or if other interface controlthus the only one that would need to be changed if one

7

The Code!

– You start by creating an instance of the Mediator andthe Mediator.Each class needs to be aware of the existence of

its constructor.then pass the instance of the Mediator to each class in

KTextField

The text field registers itself with the mediator.

13

The Mediator Code!

– It supports the Copy, Clear and Select methods, and hasThe Mediator itself is very simple.

register methods for each of the controls.

More Mediator Code!

15

Yet More Mediator Code!

System Initialization

is the initialization of all the controls to the desired state.One further operation that is best delegated to the Mediator

the program evolves, we simply create anknown, default state, and since these states may change asWhen we launch the program, each control must be in a

(^) init

(^) method in

the Mediator, which sets them all to the desired state.

public void init() {Clear button and we simply call that method.In this case, that state is the same as is achieved by the

Clear();

}

17

Concluding Remarks

  • objects in a program.The Mediator makes loose coupling possible between among several objects.It also localizes the behavior that otherwise would be distributed

changing the Mediator.You can change the behavior of the program by simply

part of the program.Colleagues to a system without having to change any otherThe Mediator approach makes it possible to add new

in the rest of a user interface.needing to know too much about the objects and methodsThe Mediator solves the problem of each Command object

More Concluding Remarks

– Sometimes you can improve this situation by revisinghard to change and maintain.The Mediator can become complex, making it

the responsibilities you have given the Mediator.

  • Each object should carry out it’s own tasks and the objects.Mediator should only manage the interaction between
  • Colleague has available.for each Colleague to call and knows what methods eachEach Mediator is a custom-written class that has methods This makes it difficult to reuse Mediator code in different projects.

interactions any other way.this code is far easier than managing the complex objectOn the other hand, most Mediators are quite simple and writing