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

折月煮酒 提交于 2019-11-30 10:15:35

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!