Emeka Egbuonye CSPP 51023 March 02,2010 The Mediator Pattern.

Slides:



Advertisements
Similar presentations
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that 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.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Design Pattern: Mediator Mediator Modified from Kyle Kimport’s: Design Patterns: Mediator Design Patterns: Mediator Ref:
Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:
A Behavioral Pattern. Problem: Simple enough right? Object.
Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.
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.
Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
Software Engineering I Object-Oriented Design Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Design Patterns Ric Holt & Sarah Nadi U Waterloo, March 2010.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
BY VEDASHREE GOVINDA GOWDA
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Design Patterns.
A Behavior Object Pattern
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
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.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 11 Subsystem Design.
BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
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.
Refactoring1 Improving the structure of existing code.
Facade Introduction. Intent Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
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.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
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.
Builder An Object Creational Pattern Tim Rice CSPP51023 March 2, 2010.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
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.
1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Refactoring1 Improving the structure of existing code.
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.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Design Patterns: MORE Examples
Mediator Design Pattern
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Low Budget Productions, LLC
Presented by Igor Ivković
Mediator Design Pattern (Behavioral)
Informatics 122 Software Design II
Mediator.
Strategy Design Pattern
Informatics 122 Software Design II
Presented by Igor Ivković
Presentation transcript:

Emeka Egbuonye CSPP March 02,2010 The Mediator Pattern

1 The Mediator pattern provides a central hub to guide the interactions between many objects Key participants in the Mediator pattern  Communicates with other Colleagues through its Mediator  Defines the interface for communication between Colleague objects Mediator  Implements the Mediator interface and coordinates communication between Colleague objects It is aware of all the Colleagues and their purpose with regards to inter communication.  The Mediator is a behavioral design pattern that provides a central hub to guide the interactions between many objects. According to Gamma et al, the intent of the Mediator pattern is to "Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently." (Gamma et al., 1995)Gamma et al., 1995  This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior Concrete Mediator Concrete Colleague Similar / Related pattern(s)  Facade: Mediator is similar to Facade because it abstracts functionality of existing classes  Observer: Mediator and Observer are competing patterns. Their differences  Observer distributes communication by introducing “observer” and “subject” objects; Mediator object encapsulates the communication between other objects

2 Problems associated with large number of classes can be solved by the “Mediator” pattern  A vicious circle created by too many interactions between objects to the point that every object has a relationship with every other object An object may be unable to work without the support of a lot of other objects  resulting in same mess you just tried to un-entangle! Where the behavior is shared among different objects, a simple change may necessitate defining a lot of subclasses  Communication between a large number of classes As more classes are developed in a program, (especially during maintenance and/or refactoring), communication between these classes can become more complex Results in increased difficulty in maintaining and reading the program  Nee d for efficient system design Designing reusable components are more efficient but dependencies between the potentially reusable pieces demonstrate the “spaghetti code” phenomenon Problems that can be solved by the “Mediator” Solutions offered by the “Mediator” pattern  Helps to model a class whose object at run-time is responsible for controlling and coordinating the interactions of a group of other objects Communication between objects is encapsulated with a mediator object  Boosts system efficiency as objects no longer need to know about each other, they just need to know their Mediator As a result, these objects no longer communicate directly with each other, but instead communicate through the mediator  Increases design freedom Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and allowing the designer to vary their interaction independently

3 A number of real life examples demonstrates the application of the “Mediator” pattern Air Traffic Control e.g., O’Hare The Stock Exchange (e.g., NYSE) Speaker of the House (e.g., U.K. Parliament) ExampleDetail  No air traffic controllers means every aircraft must know about every other aircraft to avoid collisions Impossible to all other aircrafts in the vicinity  As a remedy, air traffic controller acts as the Mediator Aircrafts do not need to communicate with each other directly All aircrafts send the ATC their flight data The ATC decides other aircrafts to be informed.  The Stock Exchange acts as the “Mediator” Traders do not need to know other traders to buy and sell The exchange matches traders’ deals and settlements  The Speaker of the House of Commons acts as the “Mediator” Presides over the House's debates, determining which members may speak Maintains order during debates and may punish members who break the rules of the House MPs (colleagues) may never address each other directly UML structure of the Air Traffic Control example AirTrafficControllerOHareIntlATCAircraftCommercialJet1PassengerJet1 + Mediator

4 Code for stock exchange example (1/2) The stock exchange acts like a mediator and the traders do not need to know other traders and services provided by them to make a deal. The traders have their own responsibilities as buyers and sellers and it is stock exchange’s responsibility to match their deals and get the settlement done Let’s look at the code part: /** * StockExchange – this is the mediator class */ public class StockExchange { public static void doTransaction (String typeOfTransaction, int quantity, Scrip scrip, Trader trader) { Transaction transaction = new Transaction(typeOfTransaction, quantity, scrip, trader); // try and match the current transaction // with the ones saved in DB and find out // whether a counter transaction is there or // are there many such transactions which could // fulfill requirement of this transaction. matchTransaction(transaction) } public static getPrice (Scrip scrip) { // try and match this transaction with all // the saved ones. If they match till whatever extent // trade for that. Then save, with status Traded for // number of shares traded and save the rest as New. } }// End of class

5 Code for stock exchange example (2/2) /** * Trader1 – this trader wants to sell 100 shares of company XYZ */ public class Trader1 { public void doTransaction (String typeOfTransaction, int quantity) { int expectedPrice = 200; Scrip scrip = new Scrip(“XYZ”); int price = StockExchange.getPrice(scrip); if(typeOfTransaction.equals(“SELL”)){ if(price >= expectedPrice){ StockExchange.doTransaction(“SELL”, 100, scrip, trader1); } }else if(typeOfTransaction.equals(“BUY”)){ if(price <= expectedPrice){ StockExchange.doTransaction(“BUY”, 100, scrip, trader1); } } } }// End of class /** * Trader2 – this trader wants to buy 100 shares of company XYZ */ public class Trader2 { public void doTransaction (String typeOfTransaction, int quantity) { int expectedPrice = 200; Scrip scrip = new Scrip(“XYZ”); int price = StockExchange.getPrice(scrip); if(typeOfTransaction.equals(“SELL”)){ if(price >= expectedPrice){ StockExchange.doTransaction(“SELL”, 100, scrip, trader2); } }else if(typeOfTransaction.equals(“BUY”)){ if(price <= expectedPrice){ StockExchange.doTransaction(“BUY”, 100, scrip, trader2); } } } }// End of class

Conclusion The main features of a mediator (based on earlier example code & UML illustration):  Responsibilities to individual objects  Mediator is the only smart delegating object  Transparency for other objects  If more objects join in, only place of registration is Mediator, other objects do not need to know anything about the new object  The code becomes very maintainable It would be difficult to deny the sheer simplicity and usefulness of the Mediator pattern. By providing the loose coupling between Colleague objects and centralizing the interaction logic in the Mediator, changes are more easily facilitated, and code clarity is heightened