Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM Light Toolkit - Messenger uses Event Aggregator or Mediator Pattern?

Can someone please help me sought out if I/Messenger class (and implementation) from MVVM light toolkit demonstrates usage of Event Aggregator Pattern or Mediator Pattern?

If someone proposes that it is following partially both the patterns then I request details of which part of implementation resembles which pattern to keep the answer valid.

Ref: One comparison between the two pattern, which is absolutely interesting.

like image 467
Jsinh Avatar asked Mar 30 '14 18:03

Jsinh


1 Answers

I think the Messenger isn't a pure Event Aggregator nor a pure Mediator. If I where to choose between one of them I'd go for the Mediator. Lets do a little comparison.

Messenger

The Messenger facilitates sending messages between loosely coupled objects. Consumers interested in receiving messages can register for those messages. Producers can use the Messenger to broadcast messages:

void Register<TMessage>(object recipient, Action<TMessage> action);
void Send<TMessage>(TMessage message);

If the consumer is only interested in messages through a certain channel, the consumer should supply a token while registering. The producer should send messages through that channel using the same token:

void Register<TMessage>(object recipient, object token, Action<TMessage> action);
void Send<TMessage>(TMessage message, object token);

This means that the messenger has some logic in it that determines to which subscribers a message should be sent.

Event Aggregator

The purpose of an Event Aggregator is to simply listening to events from a lot of objects. It can be used to aggregate events as well. This means that the Event Aggregator subscribes to events from publishers but sends his own events to its subscribers.

Mediator

The essence of the Mediator Pattern is to "Define an object that encapsulates how a set of objects interact" This means that the Mediator not only receives messages from publishers and sends them to subscribers but can execute logic on the received messages as well.

So?

In my opinion the Messenger isn't an Event Aggregator because its purpose is not to simplify handling events. On the other hand I don't think it is a Mediator either because it's purpose is not to decide how objects interact nor to impose logic on the communication. If I where to choose though, I'd say the Messenger is a Mediator because it has a little logic to send messages through channels.

like image 149
venerik Avatar answered Sep 22 '22 12:09

venerik