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

1

5

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.

Ruhnke answered 30/3, 2014 at 18:23 Comment(0)
E
15

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.

Evanevander answered 3/4, 2014 at 20:35 Comment(1)
The implementation is more influenced by Mediator pattern offering specialized channels for sending message. Though the channels are provided for sake of "Aggregating" message communication more situationally using the same types for communication and has no part to define how objects interact or impose logic on this interaction via the Messenger. On the other hand base implementation (if 'tokens' not considered) fully complies to proposed Aggregator pattern. To conclude and not forced to choose :) "Messenger is mixed, compatible and merged implementation of Mediator & Aggregator patterns".Ruhnke

© 2022 - 2024 — McMap. All rights reserved.