加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php – Symfony2中的设计模式:EventDispatcher是一个Mediator或Event Aggreg

发布时间:2020-05-25 08:54:44 所属栏目:PHP 来源:互联网
导读:从Symfony2的EventDispatcher组件文档: The Symfony2 EventDispatcher component implements the Mediator pattern in a simple and effective way to make all these things possible and to make your projects truly ex

从Symfony2的EventDispatcher组件文档:

The Symfony2 EventDispatcher component implements the Mediator pattern in a simple and effective way to make all these things possible and to make your projects truly extensible.

我一直在阅读关于Event Aggregator和Mediator模式及其differences的内容.对我而言,事件聚合器看起来像是Mediator的一个特例,它使用事件来促进沟通,并且内部没有任何业务逻辑.另一方面,Mediator更通用,并且可以允许一些业务逻辑来决定是否应该进行某些通信.

所以我检查了Symfony2的EventDispatcher或TraceableEventDispatcher的源代码,发现可能改变通信的唯一逻辑是检查事件传播是否已经停止,如下所示:

protected function doDispatch($listeners,$eventName,Event $event)
{
    foreach ($listeners as $listener) {
        call_user_func($listener,$event,$this);
        if ($event->isPropagationStopped()) {
            break;
        }
    }
}

这是为什么Symfony2中的EventDispatcher实现Mediator模式而不是Event Aggregator模式的原因?如果检查isPropagationStopped的逻辑被移出EventDispatcher(比如说,转移到事件监听器),那么这会实现Event Aggregator吗?

Event Aggregator类似于Observer模式,Subject只是通知Observer对象有变化,无论它是什么类型的事件都需要更新.

Symfony2的EventDispatcher实现了Mediator模式,因为它的作用类似于路由器,决定了哪个事件将由可以订阅多个事件的侦听器触发.如您所见,即使删除isPropagationStopped部分,EventDispatcher仍需要事件名称来确定要触发的事件.

安东尼费拉拉有一个很棒的blog post讨论这个问题

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读