Mediator.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Design Pattern: Mediator Mediator Modified from Kyle Kimport’s: Design Patterns: Mediator Design Patterns: Mediator Ref:
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
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ć
Façade Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
BY VEDASHREE GOVINDA GOWDA
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Design Patterns.
A Behavior Object Pattern
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
Emeka Egbuonye CSPP March 02,2010 The Mediator Pattern.
Case Studies on Design Patterns Design Refinements Examples.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
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.
CSC 480 Software Engineering Design With Patterns.
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.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Behavioral Pattern: Mediator C h a p t e r 5 – P a g e 169 When a program is made up of several classes, the logic and computation is divided among these.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Observer Pattern Context:
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Presented by FACADE PATTERN
Mediator Design Pattern
Strategy Design Pattern
CHAPTER 5 GENERAL OOP CONCEPTS.
Chapter 5:Design Patterns
Observer Design Pattern
Observer Design Pattern
Design Patterns Based on slides provided by Abbie Jarrett
Architectural Patterns for Interactive Software
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Presented by Igor Ivković
Multiuser Protection and the Mediator Pattern
Mediator Design Pattern (Behavioral)
Informatics 122 Software Design II
Object Oriented Design Patterns - Structural Patterns
CSC 480 Software Engineering
Review: Design Pattern Structure
An Introduction to Software Architecture
Design Patterns Lecture part 1.
8. Observer Pattern SE2811 Software Component Design
Informatics 122 Software Design II
CSC 480 Software Engineering
Presented by Igor Ivković
Software Design Lecture : 40.
Presentation transcript:

Mediator

Law of Demeter “… the methods of a class should not depend in any way on the structure of any class, except the immediate (top-level) structure of their own class. Further, each method should send messages to objects belonging to a very limited set of classes only.” OOAD, page 116

Why Mediator? Common problem: Multiple objects of same or different classes must communicate/interact. Multiple dependencies complicate objects, lead to “spaghetti code.” Solution: Define an object that encapsulates how a set of objects interact and interconnect. Make that object the hub of communications, responsible for controlling and coordinating the interactions of clients – the colleague objects.

Analogies/Metaphors Air traffic control Stock market eBay No analogy fits perfectly, but think of an airplane approaching an airport as an object. Its state includes its location, heading and speed. It would need to communicate its state to other airplane objects and to know the state of all other planes in the same airspace – a task that would distract pilots from other aspects of flying the plane – except we have created a Mediator. Planes reports to a single authority – the object we call Air Traffic Control – and that mediator has responsibility for ensuring that two planes don’t try to occupy the same space.

Advantages/Uses Partition system into smaller objects. Simplifies classes/objects by placing inter-object communications in mediator. Promotes loose coupling. Clarify complex relationships. Limit subclasses. Improve object reusability. Simplify object protocols. Facilitates refinement by containing changes in mediator or single colleagues rather than requiring updates in all classes. We want loose coupling, of course, because strong coupling complicates a system. The more an element (module) is interrelated with other modules the harder it is to understand, change or correct. Complexity can be reduced by designing systems with the weakest possible coupling between modules. Limit subclasses – behaviors that might require subclasses may be assigned to Mediator. Protocols – ways in which object can act and react – limited to few core operations.

Dangers Mediator can become monolithic, violating proscription against “God” or manager classes and making it hard to maintain.

Sample Code Interface Command{ void execute(); } Class Mediator{ BtnView btnView; BtnSearch btnSearch; BtnBook btnBook; LblDisplay show; void registerView(BtnView v) { btnView = v; ) void registerSearch(BtnSearch s) { btnSearch = s; void registerBook (BtnBook b) { btnBook = b; void registerDisplay(LblDisplay d) { show = d; void book() { btnBook.setEnable(false); btnView.setEnabled(true) btnSearch.setEnabled(true); show.setText(“booking…”); If you have a complex GUI, whenever a button has been clicked, the related actions should be disabled or enabled. You may design a Mediator class to include all related classes:

continued void search() { btnSearch.setEnabled(false btnView.setEnabled(true); btnBook.setEnabled(true); show.setText("searching..."); } class BtnView extends JButton implements Command { Mediator med; BtnView(ActionListener al, Mediator m) { super("View"); addActionListener(al); med = m; med.registerView(this); public void execute() { med.view(); class BtnSearch extends JButton implements Command { BtnSearch(ActionListener al, Mediator m) { super("Search"); med.registerSearch(this); med.search(); class BtnBook extends JButton implements Command { BtnBook (ActionListener al, Mediator m) { super("Book"); med.registerBook(this); From the above design, you can see that the relationships among the classes, which also known as colleagues or participating classes, are multidirectional. Mediator class contains all the information about these classes and knows what these classes are going to do. The participating classes have to register themselves to the Mediator class.The MediatorDemo class will show the cooperation among the classes.

continued super("Just start..."); med = m; med.registerDisplay(this); setFont(new Font("Arial",Font.BOLD,24)); } class MediatorDemo extends JFrame implements ActionListener { Mediator med = new Mediator(); MediatorDemo() { JPanel p = new JPanel(); p.add(new BtnView(this,med)); p.add(new BtnBook(this,med)); p.add(new BtnSearch(this, med)); getContentPane().add(new LblDisplay(med), "North"); getContentPane().add(p, "South"); setSize(400,200); setVisible(true); public void actionPerformed(ActionEvent ae) { Command comd = (Command)ae.getSource(); comd.execute(); public static void main(String[] args) { new MediatorDemo(); java MediatorDemo

UML

UML

Related Patterns Façade – provides a unified interface to a set of interfaces to make a subsystem easier to use, but Façade does not add functionality and is not known by the subsystem classes. Observer – defines a one-to-many dependency between objects so that when one object changes state all dependents are notified and updated automatically. Observer distributes communication by introducing “observer” and “subject” classes; mediator encapsulates communication between other objects.

Sources http://www.javacamp.org/designPattern/mediator.html http://www.fluffycat.com//javasource/javapatterns/Dv dLowercaseTitle.txt http://www.vincehuston.org/dp/mediator.html http://www.patterndepot.com/put/8/mediator.pdf Design Pattern, GoF OOAD, Booch et al