Mediator - PowerPoint PPT Presentation

About This Presentation
Title:

Mediator

Description:

Mediator is one of the 23 Design Patterns which were selected by the GoF ... Mediator defines an object that encapsulates how a set of ... ( Spaghetti code) ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 31
Provided by: Prese107
Category:

less

Transcript and Presenter's Notes

Title: Mediator


1
Mediator
  • Kensho Tsuchihashi

2
Table of Contents
  1. What is Mediator?
  2. What problem does Mediator solve?
  3. Advantage and Disadvantage
  4. Additional information
  5. Structure
  6. Example
  7. Code

3
1. What is Mediator?
Mediator is one of the 23 Design Patterns which
were selected by the GoF (Gang of Four).
Purpose
Behavior
Structure
Creation
Interpreter Template
Scope
Factory Method
Interpreter Template
Class
Abstract Factory Builder Prototype Singleton
Chain of Responsibility Command Iterator Mediator
Memento Observer State Strategy Visitor
Objects
Adapter Bridge Composite Decorator Façade Flyweigh
t Proxy
4
1. What is Mediator?
Mediator - Somebody who tries to settle a
dispute between other parties.
In this context..
Mediator - Pattern that defines simplified
communication between classes.
5
1. What is Mediator?
More formally.. - Mediator defines an object
that encapsulates how a set of objects interact.
Mediator promotes loose coupling by keeping
objects from reffering to each other explicitly,
and it lets you vary their interaction indepently.
6
2. What problem does Mediator solve?
What if every objects communicate to each other
one by one?
7
2. What problem does Mediator solve?
Existence of Mediator makes things much simpler.
8
2. What problem does Mediator solve?
Motivation
Object-Oriented Design
  • Encourages the distribution of behavior among
    objects.
  • As a result, an object structure tends to be many
    connections between objects in the worst case,
    every object ends up knowing each other.
    (Spaghetti code)

Mediator
  • Is responsible for controlling and coordinating
    the interactions for a group from referring to
    each other explicitly.

9
2. What problem does Mediator solve?
Applicability
Use Mediator when
  • A set of objects communicate in well-defined but
    complex ways. The resulting interdependencies are
    unstructured and difficult to understand.
  • Reuse an object is difficult because it refers to
    and communicates with many other objects.
  • A behavior thats distributed between several
    classes should be customizable without a lot of
    subclassing.

10
3. Advantage and Disadvantage
3-1. Advantage
  • Mediator helps clarify how objects interact in a
    system.

3-2. Disadvantage
  • Mediator pattern trades complexity of interaction
    for complexity in the mediator.
  • Mediator can become more complex than any
    individual colleague, which makes mediator itself
    hard to maintain.

11
4. Additional information
Analogies
  • Conductor
  • Meeting scheduler
  • Airport control tower
  • Traffic controller
  • Stock Market

12
5. Structure
In UML
Mediator
13
6. Example
Login Dialog
  • Select Guest or Login.
  • When user login, input username and
    password.
  • Press OK or Cancel.

.. Looks so simple
14
6. Example
But what if with these requirements?
  • When Guest is selected, usernameand
    password should be disabled.
  • When Login is selected, username should be
    enabled.
  • When there is no character in username,
    password should be disabled.
  • When user inputs any characters in username,
    password should be enabled.
  • When there is any character both in username
    and password, OK should be enabled. If not,
    OK should be disabled. (When Guest is
    selected, OK should be enabled.)

.. pretty complicated!
15
See real program.
16
7. Code
7-1. List of classes
No Name Description
1 Mediator Interface that defines API of mediator
2 Colleague Interface that defines API of member
3 ColleagueButton Class that represents button and implements Colleague Interface
4 ColleagueTextField Class that represents text field and implements Colleague Interface
5 ColleagueCheckbox Class that represents checkbox and implements Colleague Interface
6 LoginFrame Class that represents login dialog and implements Mediator Interface
7 Main Class that has main
17
7. Code
7-2. Class Diagram
18
7. Code
7-3. Sequence Diagram
LoginFrame
ColleagueCheckbox
ColleagueTextField
ColleagueButton
19
7. Code
7-4. (1) Mediator
public interface Mediator public abstract
void createColleagues() public abstract
void colleagueChanged()
20
7. Code
7-4. (2) Colleague
public interface Colleague public
abstract void setMediator(Mediator mediator)
public abstract void setColleagueEnabled(boolean
enabled)
21
7. Code
7-4. (3) ColleagueButton
import java.awt.Button public class
ColleagueButton extends Button implements
Colleague private Mediator mediator
public ColleagueButton(String caption)
super(caption) public void
setMediator(Mediator mediator)
this.mediator mediator public
void setColleagueEnabled(boolean enabled)
// Mediator will tell you enabled/disabled
setEnabled(enabled)
22
7. Code
7-4. (4) ColleagueTextField
import java.awt. public class
ColleagueTextField extends TextField implements
TextListener, Colleague private Mediator
mediator public ColleagueTextField(String
text, int columns) super(text,
columns) public void
setMediator(Mediator mediator)
this.mediator mediator public void
setColleagueEnabled(boolean enabled) //
Mediator will tell you enabled/disabled
setEnabled(enabled) setBackground(enable
d ? Color.white Color.lightGray)
public void textValueChanged(TextEvent e)
// If texfield changed, report to Mediator
mediator.colleagueChanged()
23
7. Code
7-4. (5) ColleagueCheckbox
import java.awt. public class
ColleagueCheckbox extends Checkbox implements
ItemListener, Colleague private Mediator
mediator public ColleagueCheckbox(String
caption, CheckboxGroup group, boolean state)
super(caption, group, state)
public void setMediator(Mediator mediator)
this.mediator mediator public
void setColleagueEnabled(boolean enabled)
// Mediator will tell you enabled/disabled
setEnabled(enabled) public void
itemStateChanged(ItemEvent e) // If
checkbox state changed, report to Mediator
mediator.colleagueChanged()
24
7. Code
7-4. (6) LoginFrame (1/5)
import java.awt.Frame import java.awt.Label
import java.awt.Color import java.awt.CheckboxGr
oup import java.awt.GridLayout import
java.awt.event.ActionListener import
java.awt.event.ActionEvent public class
LoginFrame extends Frame implements
ActionListener, Mediator private
ColleagueCheckbox checkGuest private
ColleagueCheckbox checkLogin private
ColleagueTextField textUser private
ColleagueTextField textPass private
ColleagueButton buttonOk private
ColleagueButton buttonCancel
25
7. Code
7-4. (6) LoginFrame (2/5)
public LoginFrame(String title)
super(title) setBackground(Color.lightGr
ay) setLayout(new GridLayout(4, 2))
// Creating grid 4 x 2 using layout manager
createColleagues()
// Creating Colleagues add(checkGuest)
// Layout from here
add(checkLogin) add(new
Label("Username")) add(textUser)
add(new Label("Password"))
add(textPass) add(buttonOk)
add(buttonCancel) colleagueChanged()
// Initial setting for
enabled/disabled pack()
show()
26
7. Code
7-4. (6) LoginFrame (3/5)
public void createColleagues()
CheckboxGroup g new CheckboxGroup()
checkGuest new ColleagueCheckbox("Guest", g,
true) checkLogin new
ColleagueCheckbox("Login", g, false)
textUser new ColleagueTextField("", 10)
textPass new ColleagueTextField("", 10)
textPass.setEchoChar('') buttonOk
new ColleagueButton("OK")
buttonCancel new ColleagueButton("Cancel")
// Setting Mediator here
checkGuest.setMediator(this)
checkLogin.setMediator(this)
textUser.setMediator(this)
textPass.setMediator(this)
buttonOk.setMediator(this)
buttonCancel.setMediator(this)
27
7. Code
7-4. (6) LoginFrame (4/5)
checkGuest.addItemListener(checkGuest)
checkLogin.addItemListener(checkLogin)
textUser.addTextListener(textUser)
textPass.addTextListener(textPass)
buttonOk.addActionListener(this)
buttonCancel.addActionListener(this)
// When Colleage notices their change, Mediator
judges if each colleague should be
enabled/disabled public void
colleagueChanged() if
(checkGuest.getState()) // Guest mode
textUser.setColleagueEnabled(false)
textPass.setColleagueEnabled(false)
buttonOk.setColleagueEnabled(true)
else // Login mode
textUser.setColleagueEnabled(true)
userpassChanged()
28
7. Code
7-4. (6) LoginFrame (5/5)
// textUser or textPass has been changed.
Mediator judges if each colleague should be
enabled/disabled private void
userpassChanged() if
(textUser.getText().length() gt 0)
textPass.setColleagueEnabled(true)
if (textPass.getText().length() gt 0)
buttonOk.setColleagueEnabled(true)
else buttonOk.setColleag
ueEnabled(false) else
textPass.setColleagueEnabled(false)
buttonOk.setColleagueEnabled(false)
public void
actionPerformed(ActionEvent e)
System.out.println(e.toString())
System.exit(0)
29
7. Code
7-4. (7) Main
import java.awt. import java.awt.event.
public class Main static public void
main(String args) new
LoginFrame("Mediator Sample")
30
  • Sources
  • http//www.dofactory.com/Patterns/PatternMediator.
    aspx
  • http//sourcemaking.com/design_patterns/mediator
  • Introduction to Design Pattern in Java by
    Hiroshi Yuki
  • Design Pattern by GoF
Write a Comment
User Comments (0)
About PowerShow.com