Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mediator pattern vs Publish/Subscribe

Can someone point out the main differences between the two?

It seems that, at least conceptually, the two are very closely related. If I were to hazard a guess, I would say that the publish/subscribe method is a subset of the mediator pattern (since the mediator need not necessarily be used in the publish/subscribe manner, but the latter seems to require a sort of mediator object). Is that anywhere near close to it?

like image 483
Kristian D'Amato Avatar asked Jul 01 '10 23:07

Kristian D'Amato


2 Answers

How I would describe the difference is that in mediator you probably care if the end application receives the message. So you use this to guarantee who is receiving the message. Whereas with pub/sub you just publish your message. If there are any subscribers they will get it but you don't care.

like image 117
spinon Avatar answered Sep 19 '22 12:09

spinon


The implementation could be the same, but logically they are different (The difference is simple, but it is hard to see). I'll explain it in a simple way below.

Pratically, in the implementation of the Publish/Subscribe pattern you will have at least an object with the methods "publish" and "subscribe". But you can have also more of them, so the communication between components is not centralized by definition.

In the implementation of the Mediator pattern you will have JUST ONE object with methods "publish" and "subscribe". So the communication is "centralized" by definition.

like image 24
Gabriel Nosso Avatar answered Sep 20 '22 12:09

Gabriel Nosso