Title: Mediator
1Mediator
2Table of Contents
- What is Mediator?
- What problem does Mediator solve?
- Advantage and Disadvantage
- Additional information
- Structure
- Example
- Code
31. 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
41. 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.
51. 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.
62. What problem does Mediator solve?
What if every objects communicate to each other
one by one?
72. What problem does Mediator solve?
Existence of Mediator makes things much simpler.
82. 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.
92. 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.
103. 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.
114. Additional information
Analogies
- Conductor
- Meeting scheduler
- Airport control tower
- Traffic controller
- Stock Market
125. Structure
In UML
Mediator
136. Example
Login Dialog
- Select Guest or Login.
- When user login, input username and
password. - Press OK or Cancel.
.. Looks so simple
146. 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!
15See real program.
167. 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
177. Code
7-2. Class Diagram
187. Code
7-3. Sequence Diagram
LoginFrame
ColleagueCheckbox
ColleagueTextField
ColleagueButton
197. Code
7-4. (1) Mediator
public interface Mediator public abstract
void createColleagues() public abstract
void colleagueChanged()
207. Code
7-4. (2) Colleague
public interface Colleague public
abstract void setMediator(Mediator mediator)
public abstract void setColleagueEnabled(boolean
enabled)
217. 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)
227. 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()
237. 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()
247. 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
257. 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()
267. 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)
277. 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()
287. 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)
297. 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