简体   繁体   English

为什么Symfony EventDispatcher对事件使用任意名称而不是基于类的传播?

[英]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.

相关问题 什么是使用Symfony的EventDispatcher组件的正确方法? - what's the proper way to use Symfony's EventDispatcher component? 基于类的“任务”模块的良好模式? - Good Pattern for Class-Based “Task” Modules? 为什么Abstract Factory使用抽象类而不是接口? - Why does Abstract Factory use abstract class instead of interface? 为什么使用单例而不是静态类? - Why use singleton instead of static class? Symfony2中的设计模式:EventDispatcher是Mediator或Event Aggregator吗? - Design pattern in Symfony2: is EventDispatcher a Mediator or Event Aggregator? 为什么策略模式使用工具而是扩展? - Why does strategy pattern use implements instead extends? symfony2中的每个类都需要是一个服务吗? - Does every class in symfony2 need to be a service? 为什么BLL层应该使用方法重载而不是使用具有单独名称的单独方法……不应该相反吗? - Why should BLL layer use method overloads instead of separate methods with individual names…shouldn't it be the other way around? 为什么 Class 适配器设计模式不能使用一个接口代替多个 Inheritance? - Why the Class Adapter Design Pattern Can't Use an Interface Instead of Multiple Inheritance? 将值传播到基类的设计模式? - Design pattern for propagation value to base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM