[英]Why does Symfony EventDispatcher use arbitrary names for events instead of class-based propagation?
I'm looking to implement Observer Pattern into my app (not php, that's why not using Symfony's component). 我想在我的应用程序中实现Observer Pattern(不是php,这就是为什么不使用Symfony的组件)。
I found it strange though that the component is not using classes to propagate or listen to events since that would allow listening on whole tree of events using inheritance. 虽然组件没有使用类来传播或监听事件,但我发现它很奇怪,因为这样可以使用继承来监听整个事件树。
I can't see the single advantage of using name-based listeners, would you help me with my pondering? 我看不到使用基于名称的听众的单一优势,你能帮我解决一下吗?
Symfony2, in my opinion, favors cohesion over coupling and that's why they use this pattern ( Mediator pattern ). 在我看来,Symfony2有利于耦合的凝聚力,这就是他们使用这种模式( Mediator模式 )的原因。
From Symfony docs : 来自Symfony文档 :
Objected Oriented code has gone a long way to ensuring code extensibility. 面向对象的代码在确保代码可扩展性方面已经走了很长的路。 By creating classes that have well defined responsibilities, your code becomes more flexible and a developer can extend them with subclasses to modify their behaviors. 通过创建具有明确定义的职责的类,您的代码变得更加灵活,开发人员可以使用子类扩展它们以修改其行为。 But if he wants to share his changes with other developers who have also made their own subclasses, code inheritance is no longer the answer. 但是如果他想与其他已经创建了自己的子类的开发人员分享他的更改,代码继承就不再是答案了。
Consider the real-world example where you want to provide a plugin system for your project. 考虑一下您希望为项目提供插件系统的实际示例。 A plugin should be able to add methods, or do something before or after a method is executed, without interfering with other plugins. 插件应该能够添加方法,或者在执行方法之前或之后执行某些操作,而不会干扰其他插件。 This is not an easy problem to solve with single inheritance, and multiple inheritance (were it possible with PHP) has its own drawbacks. 使用单继承解决这个问题不是一个简单的问题,多重继承(PHP可能)有其自身的缺点。
The Symfony2 Event Dispatcher component implements the Mediator pattern in a simple and effective way to make all these things possible and to make your projects truly extensible. Symfony2 Event Dispatcher组件以简单有效的方式实现Mediator模式,使所有这些事情成为可能,并使您的项目真正可扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.