11

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

Gaurav
  • 3
  • 2
simon.denel
  • 780
  • 6
  • 23

8 Answers8

6

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.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • 3
    Nice parallel, but "Mediator" is a pattern of object design, so an actual example should be about objects rather than servers and sockets imho. – Rinke Mar 12 '18 at 11:51
  • @Rinke AFAIK a pattern is a far more abstract model. What we do using a pattern, is really 'an' implementation not 'the' implementation. You may read this nice answer given by Matt for one of my questions. :)) https://stackoverflow.com/a/37671309/5715934 – Supun Wijerathne Mar 12 '18 at 12:16
  • 2
    Of course, but it's a pattern of object design, not service design. I do see how you can generalize the original pattern and I like your answer. I just wanted to point out that it's not an implementation of the pattern described in the GoF book. – Rinke Mar 12 '18 at 12:26
  • @Rinke I agree with the point that it's not what describe in the GoF book. :)) – Supun Wijerathne Mar 12 '18 at 13:29
6

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

dstar55
  • 938
  • 6
  • 11
4

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.

Community
  • 1
  • 1
jaco0646
  • 15,303
  • 7
  • 59
  • 83
1

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.

Riaan Nel
  • 2,425
  • 11
  • 18
1

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

Jan Żankowski
  • 8,690
  • 7
  • 38
  • 52
0

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.

Gaurav
  • 3
  • 2
  • A mediator is not a message broker. "The mediator topology is useful for events that have multiple steps and require some level of orchestration to process the event.". "The broker topology differs from the mediator topology in that there is no central event mediator; rather, the message flow is distributed across the event processor components in a chain-like fashion through a lightweight message broker (e.g., ActiveMQ, HornetQ, etc.)." https://www.oreilly.com/library/view/software-architecture-patterns/9781491971437/ch02.html – User128525 Sep 15 '20 at 07:00
0

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

devoxy
  • 159
  • 1
  • 10
0

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.

kris
  • 979
  • 11
  • 15