+ All Categories
Home > Documents > Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem...

Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem...

Date post: 14-Jan-2016
Category:
Upload: wilfred-thomas-gordon
View: 224 times
Download: 2 times
Share this document with a friend
30
Mediator Kensho Tsuchihashi
Transcript
Page 1: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Mediator

Kensho Tsuchihashi

Page 2: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 2Mediator

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

Page 3: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 3Mediator

1. What is Mediator?

Mediator is one of the 23 Design Patterns which were selected by the GoF (Gang of Four).

Purpose

BehaviorStructureCreation

InterpreterTemplate

Scope

Factory Method

Chain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategyVisitor

InterpreterTemplate

Class

Objects

AdapterBridgeCompositeDecoratorFaçadeFlyweightProxy

Abstract FactoryBuilderPrototypeSingleton

Page 4: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 4Mediator

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.

Page 5: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 5Mediator

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.

Page 6: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 6Mediator

2. What problem does Mediator solve?

What if every objects communicate to each other one by one?

Page 7: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 7Mediator

2. What problem does Mediator solve?

Existence of Mediator makes things much simpler.

Page 8: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 8Mediator

Object-Oriented Design

Motivation

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.

2. What problem does Mediator solve?

Page 9: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 9Mediator

Use Mediator when

Applicability

2. What problem does Mediator solve?

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 that’s distributed between several classes should be customizable without a lot of subclassing.

Page 10: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 10Mediator

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.

3. Advantage and Disadvantage

Page 11: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 11Mediator

Conductor Meeting scheduler Airport control tower Traffic controller Stock Market

4. Additional information

Analogies

Page 12: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 12Mediator

5. Structure

In UML

ConcreteColleague2ConcreteColleague2ConcreteMediator

ColleagueMediator+Mediator

Page 13: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 13Mediator

6. Example

Login Dialog

Select “Guest“ or “Login“.

When user login, input “username“ and “password“.

Press “OK“ or “Cancel“.

.. Looks so simple

Page 14: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 14Mediator

6. Example

But what if with these requirements?

When “Guest“ is selected, “username“and “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!

Page 15: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 15Mediator

See real program.

Page 16: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 16Mediator

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

Page 17: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 17Mediator

7. Code

7-2. Class Diagram<<interface>>

Colleague

setMediatorsetColleagueEnabled

ColleagueTextField

mediatorsetMediatorsetColleagueEnabledtextValueChanged

TextField

Frame

CheckboxColleagueCheckbox

mediatorsetMediatorsetColleagueEnableditemStateChanged

ColleagueButton

mediatorsetMediatorsetColleagueEnabled

<<interface>>Mediator

createColleaguecolleagueChanged

LoginFrame

checkGuestcheckLogintextUsertextPassbuttonOkbuttonCancel

createColleaguecolleagueChangeduserpassChangedactionPerformed

Button

Page 18: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 18Mediator

7. Code

7-3. Sequence Diagram

: LoginFrame : ColleagueCheckbox : ColleagueTextField : ColleagueButton

Page 19: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 19Mediator

7. Code

7-4. (1) Mediator

public interface Mediator {

public abstract void createColleagues();

public abstract void colleagueChanged();

}

Page 20: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 20Mediator

7. Code

7-4. (2) Colleague

public interface Colleague {

public abstract void setMediator(Mediator mediator);

public abstract void setColleagueEnabled(boolean enabled);

}

Page 21: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 21Mediator

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);

}

}

Page 22: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 22Mediator

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(enabled ? Color.white : Color.lightGray);

}

public void textValueChanged(TextEvent e) { // If texfield changed, report to Mediator

mediator.colleagueChanged();

}

}

Page 23: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 23Mediator

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();

}

}

Page 24: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 24Mediator

7. Code

7-4. (6) LoginFrame (1/5)

import java.awt.Frame;

import java.awt.Label;

import java.awt.Color;

import java.awt.CheckboxGroup;

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;

Page 25: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 25Mediator

7. Code

7-4. (6) LoginFrame (2/5)

public LoginFrame(String title) {

super(title);

setBackground(Color.lightGray);

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();

}

Page 26: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 26Mediator

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);

Page 27: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 27Mediator

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();

}

}

Page 28: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 28Mediator

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() > 0) {

textPass.setColleagueEnabled(true);

if (textPass.getText().length() > 0) {

buttonOk.setColleagueEnabled(true);

} else {

buttonOk.setColleagueEnabled(false);

}

} else {

textPass.setColleagueEnabled(false);

buttonOk.setColleagueEnabled(false);

}

}

public void actionPerformed(ActionEvent e) {

System.out.println(e.toString());

System.exit(0);

}

}

Page 29: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 29Mediator

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");

}

}

Page 30: Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.

Page 30Mediator

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


Recommended