Mediator Pattern

a.k.a. Broker a.k.a. Vermittler

1 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Beschreibung Hochschule Konstanz

Klifikilassifikation: Obje kt bas iertes Verhlhaltensmuster

ZkZweck: Wenn eine Menge von Obje kten unttiereinan der kommunizieren wird eine Indirektion eingebaut d.h. die Objekte reden nicht mehr direkt miteinander sondern indirekt über den „Mediator“ / „Vermittler“.

"Define an object that encapsulates how a set of objects interact. Mediator promotes by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently." (Gamma et al., 1995)

2 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Motivation Hochschule Konstanz

Vorher: Kollege Kollege

Kollege

Kollege Kollege

3 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Motivation Hochschule Konstanz

Nachher: Kollege Kollege

Vermittler Kollege

Kollege

Kollege

4 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Motivation / Beispiel Hochschule Konstanz

Chat: Viele Anwender (Collegues) unterhalten sich: ‐ Ungünstig alle untereinander zu verbinden ‐ MditMediator iitst der Cha troom

Fluglotsen: ‐ Flugzeuge (Collegues) können sich nicht alle untereinander verständigen Æ Kontrollcenter

5 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Struktur Hochschule Konstanz

6 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Struktur Hochschule Konstanz

Beispiel Flugverkehr:

7 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Struktur Hochschule Konstanz Beispiel Flugverkehr:

8 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Teilnehmer Hochschule Konstanz

Mediator ‐ defines the interface for communication between Colleague objects Concrete 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. Concrete Colleague ‐ communicates with other Colleagues through its Mediator

9 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Beschreibung II Hochschule Konstanz

Abstrakte Vermittler / Interface Abstrakte Mediatoren /Interfaces werden nicht benötigt wenn nur ein Mediator verwendet wird. Sie werden benötigt wenn mehrere unterschiedliche Mediatoren zum Einsatz kommen.

10 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Pro vs. Kontra Hochschule Konstanz

Pro: ‐ Einfacheres Verständnis der Logik im System da es an einer zentralen Stelle gesammelt ist ‐ Lose Kopplung: Es können leicht neue Kollegen hinzugefügt, entfernt oder geändert werden. ‐ Nur noch 1‐n Beziehungen statt m‐n. ‐ Einfach erweiterbar: wenn neue Logik benötigt wird kann von dem Mediator/Vermittler einfach vererbt werden Kontra: ‐ Bei vielen Teilnehmern kann es zu sehr kompliziertem Inhalt/Logik des Mediators kommen Æ schwierig zu verstehen bzw. zu ändern ‐ Sing le point of filfailure: wenn der VilVermittler ausfällt, fällt das gesamte System aus ‐ Skalierbarkeit

11 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Verwandte Muster Hochschule Konstanz

Facade Pattern ‐ a simplified mediator becomes a if the mediator is the only active class and the colleagues are passive classes. A facade pattern is just an implementation of the mediator pattern where mediator is the only object triggering and invoking actions on passive colleague classes. The Facade is being called by some external classes. ‐ the mediator patter just "mediate" the requests between the colleague classes. It is not supposed to change the messages it receives and sends; if it alters those messages then it is an Adapter pattern. ‐ the observer and media tor are siilimilar patterns, solving the same problem. The main difference between them is the problem they address. The observer pattern handles the communication between observers and subjects or subject. It's very probable to have new observable objects added. On the other side in the mediator pattern the mediator class is the the most likely class to be inherited.

12 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz Danke für die Aufmerksamkeit!

13 © 2009, Sebastian Gäng, Moritz Moll, Design Pattern, HTWG Konstanz