1 / 22

Mediator

Mediator. Abbas Rasoolzadegan. فرم نمونه براي رزور يک اتاق مهماني در يک هتل. Interactions. Next Example: a dialog box in a graphical user interface. Problem. Object oriented design encourages the distribution of behavior among the various objects increasing reusability ( high cohesion )

gabrielsonm
Download Presentation

Mediator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mediator Abbas Rasoolzadegan Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  2. فرم نمونه براي رزور يک اتاق مهماني در يک هتل Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  3. Interactions Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  4. Next Example: a dialog box in a graphical user interface 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 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 Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  7. interactions between the objects of a dialog box through a mediator Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  8. Sequence Diagram Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  9. Events: • The list box tells its director that it has changed. • The director gets the selection from the list box. • The director passes the selection to the entry field. • Now that the entry field contains some text, the director enables button(s) for initiating an action. Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  10. Class Diagram Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  11. 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. Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  13. Structure Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  14. A typical object structure 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 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 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 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) Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  19. 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<char*>* 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(); } Sample Code 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 } } 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 Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

  22. Questions???? Thank You!! Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory

More Related