Design Pattern: Mediator Mediator Modified from Kyle Kimport’s: Design Patterns: Mediator Design Patterns: Mediator Ref:

Slides:



Advertisements
Similar presentations
Design Patterns.
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.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
A Behavioral Pattern. Problem: Simple enough right? Object.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Façade Pattern Jeff Schott CS590L Spring What is a façade? 1) The principal face or front of a building 2) A false, superficial, or artificial appearance.
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
Cam Quach Joel Derstine Mediator: Object Behavioral.
Mediator A Behavioral Design Pattern for the New Millennium Cory Nugent.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Chapter 22 Object-Oriented Design
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Façade Design Pattern (1) –A structural design pattern.
BY VEDASHREE GOVINDA GOWDA
Inheritance using Java
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
A Behavior Object Pattern
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Emeka Egbuonye CSPP March 02,2010 The Mediator Pattern.
Case Studies on Design Patterns Design Refinements Examples.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Mediator Pattern and Multiuser Protection Billy Bennett June 8 th, 2009.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Strategy Design Patterns CS 590L - Sushil Puradkar.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Structural Design Patterns
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.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
CS616: Software Engineering Spring 2009 Design Patterns Sami Taha.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise Ise
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
Façade Design Pattern by Ali Alkhafaji Unified interface for a set of interfaces to promote readability and usability.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Mediator Design Pattern
Sections Inheritance and Abstract Classes
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Introduction to Design Patterns
Observer Design Pattern
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Presented by Igor Ivković
Multiuser Protection and the Mediator Pattern
Mediator Design Pattern (Behavioral)
Mediator.
Presented by Igor Ivković
Presentation transcript:

Design Pattern: Mediator Mediator Modified from Kyle Kimport’s: Design Patterns: Mediator Design Patterns: Mediator Ref:

Outline: What is a Mediator What is a Mediator Why a Mediator? Why a Mediator? Anatomy of Mediator Anatomy of Mediator Consequences of Using a Mediator Consequences of Using a Mediator Mediator and Rest of the World Mediator and Rest of the World Implementing a Mediator Implementing a Mediator Conclusions Conclusions

What is a mediator An object that abstracts inter-workings of two or more objects. An object that abstracts inter-workings of two or more objects. Acts as communication hub that serve to decouple objects — no need to know about each other, just need to know their Mediator. Acts as communication hub that serve to decouple objects — no need to know about each other, just need to know their Mediator. Promotes loose coupling by keeping objects from referencing to each other explicitly Promotes loose coupling by keeping objects from referencing to each other explicitly

Mediator: a system of at least 3 objects messaging in a star topology Colleague Colleague Colleague Mediator OurMediator OurMediator OurMediator Provides a common connection point, centralized (subclassable) behavior and behavior mgmt, all with a common interface

Classification and Intent Classification: Object Behavior Encapsulate object-to-object communication Keeps objects from knowing about each other directly; this allows us easily change an object’s behavior

Motivation OO-design allows for more reusable and more elegant programs, when objects need to refer to each other, this elegance is often lost. OO-design allows for more reusable and more elegant programs, when objects need to refer to each other, this elegance is often lost. By consolidating all interaction in a single class, we can regain elegance and reusability By consolidating all interaction in a single class, we can regain elegance and reusability

When to use a mediator? When one or more objects must interact with several different objects. When one or more objects must interact with several different objects. When centralized control is desired When centralized control is desired When simple object need to communicate in complex ways. When simple object need to communicate in complex ways. When you want to reuse an object that frequently interacts with other objects When you want to reuse an object that frequently interacts with other objects

Structure abstract Mediator abstract Collegue concrete Mediator concrete Collegue 1 concrete Collegue 2

Participant--Abstract Mediator Define the Collegue-to-Mediator interface Define the Collegue-to-Mediator interface Participant--Concrete Mediator Derivated from Abstract Mediator Derivated from Abstract Mediator Aware of and knows the purpose of all concrete Collegues Aware of and knows the purpose of all concrete Collegues Receives messages from a colleague & sends Receives messages from a colleague & sends necessary commands to other colleagues necessary commands to other colleagues

Participant--Abstract Colleague Define the Mediator-to- Colleague interface Define the Mediator-to- Colleague interface Knows about the mediator, but none of its colleagues Knows about the mediator, but none of its colleagues Participant--Concrete Colleagues Derived from abstract Colleague Derived from abstract Colleague Each Concrete Colleague knows its own behavior on a small scale, but NOT on a large scale. Each Concrete Colleague knows its own behavior on a small scale, but NOT on a large scale.

Mediator: Pros Limits subclassing & specialization, allowing Colleague classes to be reused w/o any changes Limits subclassing & specialization, allowing Colleague classes to be reused w/o any changes Decouples colleagues, which facilitates independent variations of the colleague and mediator classes. Decouples colleagues, which facilitates independent variations of the colleague and mediator classes. Simplifies protocol by replacing many-to-many interaction with one-to-one interaction Simplifies protocol by replacing many-to-many interaction with one-to-one interaction Abstracts object behaviors & cooperation. This allows you to deal w/ an object’s small-scale behavior separately form its interaction with other objects Abstracts object behaviors & cooperation. This allows you to deal w/ an object’s small-scale behavior separately form its interaction with other objects

Mediator: Cons Reducing the complexity of Colleagues increases the complexity of the Mediator itself. In some situations, maintaining a large Mediator may become as daunting a task as operating w/o a one.

Implementing a Mediator: Design Consideration When colleagues are general and reusable, we should use an abstract Mediator When colleagues are general and reusable, we should use an abstract Mediator When colleagues will not be used elsewhere, you may forego the Abstract Mediator When colleagues will not be used elsewhere, you may forego the Abstract Mediator Do we need an abstract Mediator?

How Colleagues Interact w/ the Mediator? Implement the mediator itself as an Observer Implement the mediator itself as an Observer Colleague pass themselves as arguments when communicating with the mediator Colleague pass themselves as arguments when communicating with the mediator

Mediator & the Rest of the World Observer: the Mediator class may be implemented using an Observer Observer: the Mediator class may be implemented using an Observer Façade: is similar to a Mediator, but with one-way communication from the Façade to its subsystem classes. Façade: is similar to a Mediator, but with one-way communication from the Façade to its subsystem classes. Related Design Pattern

Mediator: Real World Example

Sample Java Code abstract class Mediator { public abstract void colleagueChanged(Colleague c); public abstract void colleagueChanged(Colleague c);} abstract class Colleague{ private Mediator myMediator; private Mediator myMediator; public void changed(){ public void changed(){ myMediator.colleagueChanged(this); myMediator.colleagueChanged(this); }} class ListBox extends Colleague{ public void display(){ public void display(){ //..drawing routines //..drawing routines } public void mouseClick(int x, int y){ public void mouseClick(int x, int y){ //ListBox obj does sth… //ListBox obj does sth… }}

Sample Java Code class FontDialogBox extends Mediator{ private Button ok; private List fontList; private TextField fontName; //...more private CheckBox latino; private CheckBOx latin2; //...more public FontDialogBOx(){ fontList = new ListBox(this); fontName = new TextField(this); //... fontList = new ListBox(this); fontName = new TextField(this); //...} public void display(){ fontList.display(); fontList.display(); fontName.display(); //... fontName.display(); //...} public void colleagueChanged(Colleague c){ if( c==fontList) if( c==fontList) //font_list does sth… //font_list does sth… else if(c==fontName) else if(c==fontName) //fontName does sth. //fontName does sth. else if…… else if……} }} Declare Colleague Objects Objects Manipulate colleague Objects Mediator Object

Conclusion A mediator is an objcet-behavior design pattern. A mediator is an objcet-behavior design pattern. Use a Mediator when simple objects interact in complex ways Use a Mediator when simple objects interact in complex ways The Mediator pattern consists of two types of objects: the Mediator and its Colleagues The Mediator pattern consists of two types of objects: the Mediator and its Colleagues All object-to-object communication is encapsulated in the Mediator All object-to-object communication is encapsulated in the Mediator Mediator allows for greater reusability, and generally more elegant, readable code. Mediator allows for greater reusability, and generally more elegant, readable code.