Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 946
simon.denel Avatar asked Dec 21 '16 15:12

simon.denel


3 Answers

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

like image 190
dstar55 Avatar answered Nov 13 '22 11:11

dstar55


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.

like image 22
Supun Wijerathne Avatar answered Nov 13 '22 10:11

Supun Wijerathne


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.

like image 2
jaco0646 Avatar answered Nov 13 '22 11:11

jaco0646