EventListener 是 Observable 吗?

发布于 2024-11-06 04:55:37 字数 137 浏览 4 评论 0原文

我目前正在学习有关设计模式的课程,并且想知道 EventListener 是否是 Observable

我并没有真正看到它们之间的区别,因为两者都有订阅者列表,并在发生变化时通知这些订阅者。

I am currently following a class about Design Patterns and was wondering whether an EventListener is an Observable?

I don't really see a difference between them because both have a list of subscribers and notify these subscribers when something has changed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不醒的梦 2024-11-13 04:55:37

Observable 只是一个可以观察其操作的对象。因此,任何可以监听某个操作然后被告知该操作发生的东西都是一个可观察对象。

这意味着事件监听器就是其中之一。因为您可以监听事件,并且事件会立即通知您它们已经发生。

就我个人而言,当有人说 Observable 时,我认为是事件。这是我关于可观察量的简单示例。一个类似的例子是发布-订阅系统,它只是不同名称下的事件(它确实有细微不同的用例)。

An Observable is simply an object where you can observe it's actions. So anything where you can listen to an action and then be told that action occurs is an Observable.

This means an Event Listener is one. Because you can listen to events and the events immediately notify you that they have happened.

Personally when anyone says Observable I think events. This is my cookie cutter example of what observables are. A similar example would be a publish-subscribe system which is just events under a different name (it does have subtly different use case).

吾家有女初长成 2024-11-13 04:55:37

根据我的经验,事件监听器模式与观察者设计模式不同。它不仅仅是一个不同的名称或其中的一部分。

这个我得具体说说。例如,此页面提供了事件侦听器系统的ac#实现。在此系统中,侦听器使用单例类Events 注册其事件处理函数,并声明它可以处理特定类型的事件。 Events 维护一个字典,将每种类型的事件映射到其处理函数。另一方面,任何想要触发事件的类都需要通过单例的函数 Events.instance.Raise() 来实现。

这里我们可以看到 3 个区别:

首先,两种模式的目的不同:监听器设计模式是将事件检测/引发代码与事件处理代码解耦,因此当更改或添加新的事件处理代码时,不会影响其他事件处理代码;观察者设计模式是让一些对象跟随另一个对象的变化。

数据结构也不同。在监听器设计模式中,只有一个全局字典来将每种类型的事件映射到其处理方法。该映射是一对一的。在观察者模式中,每个被观察的主题都维护一个观察者列表。这种映射是一对多:一个对象服从多个观察者。这种一对多主体-观察者关系可以有多个实例。

行为不同。在侦听器设计模式中,当事件发生时,事件引发程序会通知全局中介程序(单例Events.instance),因为它没有有关事件处理程序的信息。而在观察者模式中,当发生任何变化时,被观察者直接通知所有观察者。

To my experience, an Event Listener pattern is a different thing from Observer Design Pattern. It is not just a different name or which is part of which.

I have to talk about this concretely. For example, this page gives a c# implementation of an event listener system. In this system, a listener registers its event handling function with a singleton class Events and claims that it can handle a particular type of event. Events maintains a dictionary to map each type of event to its handler function. On the other hand, any class that wants to trigger the event needs to do so through the singleton's function Events.instance.Raise().

Here we can see 3 differences:

Firstly the purpose of the two patterns are different: Listener Design Pattern is to decouple the event detection/raising code from the event handling code, so that when changing or adding new events handling codes, it does not affect other events handling codes; Observer Design Pattern is to make some objects to follow the changes of another object.

The data structure is also different. In Listener Design Pattern, there is only one global dictionary to map each type of event to its handling method. This mapping is 1-to-1. While in Observer Pattern, every observed subject maintains a list of observers. This mapping is 1-to-many: one subject to many observers. There can be multiple instances of such 1-to-many subject-observers relationships.

The behavior is different. In Listener Design Pattern, when a event happens, the event raiser notifies a global mediator (the singleton Events.instance), because it has no information about the event handlers. While in Observer Pattern, when any change happens, the observed subject directly notifies all the observers.

久光 2024-11-13 04:55:37

我自己也通过查看 JDK 的源代码做了一些更多的研究。我认为它们之间的唯一区别是 Observable 在添加 Observers 时使用同步,而 EventListener 则不使用同步(至少 AbstractButton< /code> 的则不然)。

I've done some more research myself too by looking into JDK's source code. I think the only difference between them is that an Observable uses synchronized when adding Observers and an EventListener does not (at least AbstractButton's does not).

木有鱼丸 2024-11-13 04:55:37

是的,似乎为特定事件注册侦听器的事件队列就是观察者模式的一个示例。

Yeah, it seems like an event queue where you register listeners for specific events would be an example of an Observer Pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文