Mediator Pattern Mediator Pattern
a.k.a. Broker
a.k.a. Vermittler
Beschreibung
Hochschule Konstanz
g
l ifik i bj k b i h l
Klassifikation: Objektbasiertes Verhaltensmuster
Z k W i M Obj kt t i d
Zweck: Wenn eine Menge von Objekten untereinander
kommunizieren wird eine Indirektion eingebaut d.h. die Objekte reden nicht mehr direkt miteinander sondern j indirekt über den „Mediator“ / „Vermittler“.
"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
objects from referring to each other explicitly, and it lets you
vary their interaction independently." (Gamma et al., 1995)
Motivation
Hochschule Konstanz
Vorher:
Kollege
Kollege
ll Kollege
Kollege Kollege
Motivation
Hochschule Konstanz
Nachher:
KollegeKollege
Kollege Vermittler
Kollege
Kollege
Motivation / Beispiel
Hochschule Konstanz
p
Chat:
Viele Anwender (Collegues) unterhalten sich: ( g )
‐ Ungünstig alle untereinander zu verbinden M di t i t d Ch t
‐ Mediator ist der Chatroom
Fluglotsen:
‐ Flugzeuge (Collegues) können sich nicht alle
‐ Flugzeuge (Collegues) können sich nicht alle
untereinander verständigen Æ Kontrollcenter
Struktur
Hochschule Konstanz
Struktur
Hochschule Konstanz
Beispiel Flugverkehr:
Struktur
Hochschule Konstanz
Beispiel
Flugverkehr:
Teilnehmer
Hochschule Konstanz
Mediator ‐ defines the interface for communication between Colleague objects
Concrete Mediator ‐ implements the Mediator interface and coordinates communication
interface and coordinates communication
between Colleague objects. It is aware of all the Colleagues and their purpose with regards to inter Colleagues and their purpose with regards to inter communication.
Concrete Colleague communicates with other Concrete Colleague ‐ communicates with other
Colleagues through its Mediator
Beschreibung II
Hochschule Konstanz
g
Abstrakte Vermittler / Interface
Abstrakte Mediatoren /Interfaces werden nicht Abstrakte Mediatoren /Interfaces werden nicht
benötigt wenn nur ein Mediator verwendet wird.
Sie werden benötigt wenn mehrere unterschiedliche
Mediatoren zum Einsatz kommen.
Pro vs. Kontra
Hochschule Konstanz
P 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. 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:
Kontra:
‐ Bei vielen Teilnehmern kann es zu sehr kompliziertem Inhalt/Logik des Mediators kommen Æ schwierig zu verstehen bzw. zu ändern
Si l i f f il d V i l fäll fäll d
‐ Single point of failure: wenn der Vermittler ausfällt, fällt das gesamte
System aus
Verwandte Muster
Hochschule Konstanz
d
Facade Pattern
‐ a simplified mediator becomes a facade pattern 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. TheFacade is being called by some external classes.
Adapter Pattern
‐ the mediator patter just "mediate" the requestsAdapter Pattern
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.Observer Pattern
th b d di t i il ttObserver Pattern
‐ the observer and mediator are similar patterns, solving the same problem. The main difference between them is the problem they address. The observer pattern handles the communication betweenobservers and subjects or subject It's very probable to have new observable 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.