Ayende @ Rahien

Refunds available at head office

Design patterns in the test of time: Mediator

The mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior.

More about this pattern can be found here.

Like the Façade pattern, I can absolutely see the logic of wanting to use a mediator. It is supposed to make it easier to work with a set of objects, because it hides their interactions.

In practice, almost all known cases are bad ones. In fact, in most systems that I have seen any association of the name to the actual pattern it is supposed to represent is not very associated at all.

The differences between façade and mediator are minute, and you would think the same advice would apply. However, while you can find a lot of usages of facades (or at least things people would call facades), there are very few real world examples of mediator pattern in use. And almost all of them carry the marks that say: “Just read GoF book, @w$0m3!!!”

Comments

Ricardo Peres
01/21/2013 04:19 PM by
Ricardo Peres

One example of a Mediator that we use everyday is the ASP.NET web forms page: it knows and performs operations in controls that exist inside of it. Don't you agree?

Trystan
01/21/2013 05:49 PM by
Trystan

Isn't a service bus, or any kind of messaging system, a mediator? I'd also consider the events and callbacks parts of jQuery an example of the mediator pattern. I've found both to be very useful. I've also written small classes hook two services together so I can test that they're wired together correctly. Isn't that a mediator?

mravinale
01/21/2013 06:25 PM by
mravinale

How about: The Messenger class of Mvvm Light? The Event Agreggator implementation of Caliburn? or this implementation in Js? http://thejacklawson.com/Mediator.js/

I think that can be very useful when you need to build loosely coupled components.

Dmitry
01/21/2013 07:40 PM by
Dmitry

I agree with Trystan that a non-distributed message/event bus or a command queue should be considered a mediator.

Judah Gabriel Himango
01/21/2013 11:06 PM by
Judah Gabriel Himango

Yep, the MessageBus/PubSub is a mediator, and it's commonly found in MVVM frameworks to communicate between view models. It's also used in web apps to communicate between components in a page.

I use Knockout.PostBox (https://github.com/rniemeyer/knockout-postbox) to do pub/sub in my web apps. Very slick. Components (e.g. observables) communicate by publishing messages on the mediator object, ko.postbox.

I even use it for application commands. So my Song object can simply call ko.postbox.publish("Play", this) to tell my application to start playing that song. It doesn't care who is listening for that command, thus decoupling the implementation from the usage. Works out nicely.

Ayende Rahien
01/22/2013 07:47 AM by
Ayende Rahien

Guys, I don't consider message passing to be mediator. It is own separate thing.

Euproric
01/24/2013 03:18 PM by
Euproric

Ayende, I would like to hear your explanation about what diferentiates Mediator and MessageBus/Event Aggregator.

Ayende Rahien
01/25/2013 07:12 AM by
Ayende Rahien

Euproric, Think about the distinction between Facade & Mediator, or Stategy & State. Those are really subtle distinctions, but they need different names, to express their differences. To call MessageBus a Mediator is the same, except the gap is FAR larger.

Chandan
01/28/2013 02:05 PM by
Chandan

What about the mediators in PureMvc framework in ActionScript -It mediates between UI and command -Command in turn interacts with Server via a Proxy

Ayende Rahien
01/28/2013 02:12 PM by
Ayende Rahien

Chandan, I am sorry, but I am not going to even try to comment on something like that. I don't have a clue about ActionScript, far less about a specific framework in ActionScript.

Comments have been closed on this topic.