Download presentation
Presentation is loading. Please wait.
Published byMyra Broome Modified over 9 years ago
1
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 1 Mediator Abbas Rasoolzadegan
2
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 2 فرم نمونه براي رزور يک اتاق مهماني در يک هتل
3
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 3 Interactions
4
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 4 Next Example: a dialog box in a graphical user interface
5
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 5 Problem Object oriented design encourages the distribution of behavior among the various objects increasing reusability (high cohesion) decreasing reusability (high coupling) through proliferating interactions every object knows about every other changing the system’s behavior will be difficult
6
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 6 Solution Use mediator design pattern Define an object,mediator, that encapsulates how a set of objects interact promotes loose coupling by keeping objects from referring to each other explicitly lets you vary their interaction independently is generally, responsible for controlling and coordinating the interactions of a group of objects Classification: Object Behavioral
7
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 7 interactions between the objects of a dialog box through a mediator
8
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 8 Sequence Diagram
9
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 9 Events: 1. The list box tells its director that it has changed. 2. The director gets the selection from the list box. 3. The director passes the selection to the entry field. 4. Now that the entry field contains some text, the director enables button(s) for initiating an action.
10
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 10 Class Diagram
11
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 11
12
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 12 Applicability Use the Mediator pattern when a set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand. reusing 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.
13
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 13 Structure
14
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 14 A typical object structure
15
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 15 Participants Mediator defines an interface for communicating with colleague objects ConcreteMediator implements cooperative behavior, knows and maintains its colleagues Colleague classes knows its mediator object, communication with mediator as opposed to other objects
16
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 16 Consequences It limits subclassing A mediator localize behavior that otherwise would be distributed among several objects. Changing this behavior requires subclassing mediator only; colleague classes can be reused as is It decouples colleagues Vary and reuse independently It simplifies object protocols Many-to-many interactions are replaced with one-to-many which are easier to understand, maintain, and extend It abstracts how objects cooperate Clarify how objects interact in a system It centralizes control
17
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 17 Disadvantage Increased complexity of the mediator. Mediator becomes a monolithic structure that is hard to maintain
18
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 18 Implementation Issues Omitting the abstract mediator class When colleagues work with only one mediator Implement Colleague-Mediator communication using observer pattern a specialized notification (see in sample code)
19
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 19 Sample Code class DialogDirector { public: virtual ~DialogDirector(); virtual void ShowDialog(); virtual void WidgetChanged(Widget*) = 0; protected: DialogDirector(); virtual void CreateWidgets() = 0; }; class Widget { public: Widget(DialogDirector*); virtual void Changed(); virtual void HandleMouse(MouseEvent& event); //... private: DialogDirector* _director; }; void Widget::Changed () { _director->WidgetChanged(this); } class ListBox : public Widget { public: ListBox(DialogDirector*); virtual const char* GetSelection(); virtual void SetList(List * listItems); virtual void HandleMouse(MouseEvent& event); //... }; class EntryField : public Widget { public: EntryField(DialogDirector*); virtual void SetText(const char* text); virtual const char* GetText(); virtual void HandleMouse(MouseEvent& event); //... }; class Button : public Widget { public: Button(DialogDirector*); virtual void SetText(const char* text); virtual void HandleMouse(MouseEvent& event); //... }; void Button::HandleMouse (MouseEvent& event) { //... Changed(); }
20
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 20 class FontDialogDirector : public DialogDirector { public: FontDialogDirector(); virtual ~FontDialogDirector(); virtual void WidgetChanged(Widget*); protected: virtual void CreateWidgets(); private: Button* _ok; Button* _cancel; ListBox* _fontList; EntryField* _fontName; }; void FontDialogDirector::CreateWidgets () { _ok = new Button(this); _cancel = new Button(this); _fontList = new ListBox(this); _fontName = new EntryField(this); // fill the listBox with the available font names // assemble the widgets in the dialog } WidgetChanged ensures that the widgets work together properly: void FontDialogDirector::WidgetChanged ( Widget* theChangedWidget ) { if (theChangedWidget == _fontList) { _fontName->SetText(_fontList->GetSelection()); } else if (theChangedWidget == _ok) { // apply font change and dismiss dialog } else if (theChangedWidget == _cancel) { // dismiss dialog }
21
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 21 Related Patterns Façade: Abstracts a subsystem of objects providing a more convenient interface Difference with mediator: Façade is an structural pattern but mediator is a behavioral pattern In Façade, Protocol is unidirectional as opposed to mediator. Mediator is a “two-way” version of façade Façade promotes weak coupling between the subsystem classes and its client but mediator promotes weak coupling between a set of objects Façade shields clients from subsystem components, but mediator shields objects of a set from each other Colleagues can communicate with the mediator using the Observer pattern
22
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 22 Questions???? Thank You!!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.