Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top