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.

Slides:



Advertisements
Similar presentations
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Advertisements

UMBC Some Additional Patterns CMSC 432. UMBC More Patterns2 Introduction Over the next few lectures we’ll have an introduction to a number of patterns.
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 1 Mediator Abbas Rasoolzadegan.
18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
Design Pattern: Mediator Mediator Modified from Kyle Kimport’s: Design Patterns: Mediator Design Patterns: Mediator Ref:
Patterns Reusable solutions to common object-oriented programming problems When given a programming problem, re-use an existing solution. Gang of Four.
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
(c) 2010 University of California, Irvine – André van der Hoek1June 29, 2015 – 08:55:05 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
Emeka Egbuonye CSPP March 02,2010 The Mediator Pattern.
CSSE 374: Introduction to Gang of Four Design Patterns
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
January 12, Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable.
Mediator Pattern and Multiuser Protection Billy Bennett June 8 th, 2009.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
DESIGN PATTERNS CSC532 Adv. Topics in Software Engineering Shirin A. Lakhani.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.
Methods: Deciding What to Design In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT. icu.ac.kr Fall 2005 ICE0575 Lecture.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
1 Design Patterns Object-Oriented Design. 2 Design Patterns 4Reuse of design knowledge and experience 4Common in many engineering disciplines 4Avoids.
Creational Patterns
What to know for the exam. Smalltalk will be used for questions, but there will not be questions about the grammar. Questions might ask – how particular.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
Proxy.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS616: Software Engineering Spring 2009 Design Patterns Sami Taha.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
7 April 2004CSci 210 Spring Design Patterns 2 CSci 210.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Mediator Design Pattern
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
Design Patterns with C# (and Food!)
object oriented Principles of software design
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
How to be a Good Developer
Software Engineering Lecture 7 - Design Patterns
Mediator Design Pattern (Behavioral)
Informatics 122 Software Design II
Mediator.
Informatics 122 Software Design II
Chapter 8, Design Patterns Singleton
CIS 644 Tues. Nov. 30, 1999 W15A … patterns.
Presentation transcript:

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 information 5.Structure 6.Example 7.Code

Mediator Page 3 1. What is Mediator? Mediator is one of the 23 Design Patterns which were selected by the GoF (Gang of Four). Purpose BehaviorStructureCreation Interpreter Template Scope Factory Method Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Interpreter Template Class Objects Adapter Bridge Composite Decorator Façade Flyweight Proxy Abstract Factory Builder Prototype Singleton

Mediator Page 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.

Mediator Page 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.

Mediator Page 6 2. What problem does Mediator solve? What if every objects communicate to each other one by one?

Mediator Page 7 2. What problem does Mediator solve? Existence of Mediator makes things much simpler.

Mediator Page 8 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?

Mediator Page 9 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.

Mediator Page Advantage  Mediator helps clarify how objects interact in a system 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

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

Mediator Page Structure In UML ConcreteColleague2 ConcreteMediatorColleagueMediator +Mediator

Mediator Page Example Login Dialog  Select “Guest“ or “Login“.  When user login, input “username“ and “password“.  Press “OK“ or “Cancel“... Looks so simple

Mediator Page 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!

Mediator Page 15 See real program.

Mediator Page Code 7-1. List of classes NoNameDescription 1MediatorInterface that defines API of mediator 2ColleagueInterface that defines API of member 3ColleagueButtonClass that represents button and implements Colleague Interface 4ColleagueTextFieldClass that represents text field and implements Colleague Interface 5ColleagueCheckboxClass that represents checkbox and implements Colleague Interface 6LoginFrameClass that represents login dialog and implements Mediator Interface 7MainClass that has main

Mediator Page Code 7-2. Class Diagram > Colleague setMediator setColleagueEnabled ColleagueTextField mediator setMediator setColleagueEnabled textValueChanged TextFieldFrameCheckboxColleagueCheckbox mediator setMediator setColleagueEnabled itemStateChanged ColleagueButton mediator setMediator setColleagueEnabled > Mediator createColleague colleagueChanged LoginFrame checkGuest checkLogin textUser textPass buttonOk buttonCancel createColleague colleagueChanged userpassChanged actionPerformed Button

Mediator Page Code 7-3. Sequence Diagram : LoginFrame: ColleagueCheckbox: ColleagueTextField: ColleagueButton

Mediator Page Code 7-4. (1) Mediator public interface Mediator { public abstract void createColleagues(); public abstract void colleagueChanged(); }

Mediator Page Code 7-4. (2) Colleague public interface Colleague { public abstract void setMediator(Mediator mediator); public abstract void setColleagueEnabled(boolean enabled); }

Mediator Page 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); }

Mediator Page 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(); }

Mediator Page 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(); }

Mediator Page 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;

Mediator Page 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(); }

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

Mediator Page 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(); }

Mediator Page 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); }

Mediator Page 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"); }

Mediator Page 30 Sources    “Introduction to Design Pattern in Java” by Hiroshi Yuki  “Design Pattern” by GoF