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

标签 design-patterns symfony observer-pattern event-dispatching

我希望在我的应用程序中实现观察者模式(不是 php,这就是为什么不使用 Symfony 的组件)。

我发现很奇怪,尽管组件没有使用类来传播或监听事件,因为这将允许使用继承来监听整个事件树。

我看不到使用基于名称的监听器的单一优势,您能帮我思考一下吗?

最佳答案

在我看来,Symfony2 更喜欢内聚而不是耦合,这就是他们使用这种模式( Mediator pattern )的原因。

来自 Symfony docs :

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.

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.

关于design-patterns - 为什么 Symfony EventDispatcher 对事件使用任意名称而不是基于类的传播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19427090/

相关文章:

java - 需要合适的安卓模式

java - 如何观察多个可观察线程

java - 我应该同步 notifyObservers 调用吗?

java - 访客模式的使用

c# - 在 C# 的父类上正确实现 IDisposable

c# - 有没有一种方法可以强制方法遵循特定的方法签名?

php - 刷新 oauth2 token google api 和 HWIOAuthBundle

Symfony2 和 Doctrine,具有 OneToOne 关系的列不能为空

Symfony2 : customize form element IDs in form collections

java - 尝试构建一个简单的 Observer-Observable