Please enable JavaScript.
Coggle requires JavaScript to display documents.
Mediator (Consequences (Simplifies object protocol - Moves away from many…
Mediator
Consequences
Simplifies object protocol - Moves away from many to many interactions to one to many interactions, which is simpler
-
Promotes loose coupling Instead of objects talking to each other, they talk to mediator. Lets colleagues and mediator vary independently
Centralizes control - Makes mediator complex, could become a monolith
Limits subclassing Since logic is concentrated in mediator, avoids subclassing of colleagues to implement interactions
Key Objects
Concrete Mediator Implements the logic of coordinating with colleague classes
Knows the colleague classes
Colleague classes - Communicate with the mediator. Instead of communicating with each other, they communicate with the mediator
-
Motivation
OO Design promotes distributed objects. Proliferation of objects leads to a lot of dependence between objects. Meant to promote reusability but ends up being a monolith, since parts cannot be independently reused. Mediator solves this problem
Problem solved by creating an object called Mediator that serves as in intermediary between different objects. It centralizes the communication and control between different objects which now no longer have to talk to each other. They talk to the mediator and the mediator talks to the allied objects
Implementation
Mediator Colleague Communication - mediator can be observer while colleagues are subjects that send notifications to mediator. Mediator can then communicate with other colleagues. Mediator can also have a special notification method that can be used by colleagues. Colleagues pass themselves as an argument to allow mediator to know which colleague has communicated
Omitting the abstract mediator class - If there is only one mediator, can omit the abstract mediator class
Applications
Dispatcher Servlet in Spring, acts as a mediator between web request, the controller objects, and the view objects. The individual controllers are unaware of each other. The view templates are unaware of the each other, and the controllers. It is the responsibility of the Dispatcher Servlet to decide with controller and which view template to utilize when building a response to a web request.
Air Traffic Controller - ATC is the mediator, planes are the colleagues
Intent
Object that centralizes communication and interactions between a group of related objects. Promotes loose coupling by preventing objects from interacting with each other directly and lets them vary independently
-