Could someone give a use-case example where the "Mediator pattern" is useful in the real world?
Radio Taxi is an example of the Mediator pattern. Taxi drivers communicate with the Mediator(Radio Taxi Call Center), rather than with each other.
When customer needs a taxi, he calls Radio Taxi Call Center. All taxis have a GPS unit which tells where the taxi is present right now, also there is a central information system which tells which taxi is available to serve the customer. The call center will contact the available taxi nearest to customer’s location and send them to serve the customer.
have a look at https://github.com/dstar55/100-words-design-patterns-java#Mediator
Mediator is an approach to add a third party object in order to control the interaction between a group of (2 or more) objects.
The simplest example you can find is the Chat Room example, where you can see a the a ChatRoom object controls the interaction between 2 (or more) User objects. In practice if you see a web-application like Facebook, it creates a web-socket for each of the chat-box you open. So actually the web-socket communicates with the mediator(server) and the client. When a group chat is happening each client is in synch with the server using dedicated web sockets.
The Gang of Four likes to draw examples from GUIs, so naturally their examples revolve around windows, buttons, text panes, list boxes, etc. If each of these widgets communicated directly with each of the others, the result would be a spiderweb of communication. Restricting each widget to communicate only with a single mediator simplifies the communication pattern. Also see this answer for a similar explanation.
For examples outside the Gang of Four, the top two answers to a question contrasting mediator with facade mention mediator as an effective pattern for logging. Also, Spring Guru mentions an example in the Spring Framework.
In Spring MVC, there is a great example of the Mediator Pattern in action with how Spring MVC uses the Dispatcher Servlet in conjunction with the controllers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With