What is an example in the real world that uses the "Mediator pattern"?

8.1k views Asked by At

Could someone give a use-case example where the "Mediator pattern" is useful in the real world?

8

There are 8 answers

4
Supun Wijerathne On BEST ANSWER

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.

0
jaco0646 On

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.

0
Riaan Nel On

One could call a messaging service an implementation of the mediator pattern.

Let's say I want to have two components in a Java application communicate via JMS - component A publishes a message containing some sort of instruction to a JMS provider; component B subcribes to that JMS provider in order to retrieve the message and execute the instruction. Component A is communicating with component B via a mediator.

There are more examples here.

0
dstar55 On

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

1
Gaurav On

Another good example would be the RabbitMQ open source message broker library which acts as "mediator" for multiple clients/applications using it.

Here, clients or applications are unaware of by whom all the message is going to be consumed by. RabbitMQ acts as mediator and publishes messages to the subscribed clients.

0
kris On

Air traffic control system is one of them.

This pattern is widely used in real world life, where you want to avoid many to many communication.

Another real world example, im most of cases in Indian wedding, there is a mediator between bride and groom, who communicate the both sides , otherwise there would be many persons taking each other and different things and no one is not knowing complete details. So, when there are many participants want to discuss one topic and avoid many communications, there mediator pattern is useful.

Facebook or any social networking site, where many people come together and talk about a topic, so in this Facebook/Social network site play as mediator role.

0
Jan Żankowski On

One could say that an ESB (Enterprise Service Bus) is essentially a large-scale application of the Mediator pattern.

0
devoxy On

Don't forget about Kafka, it's also a great example.