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

时光怂恿深爱的人放手 提交于 2019-11-30 14:19:25

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/22747954/mvvm-light-toolkit-messenger-uses-event-aggregator-or-mediator-pattern

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