观察者设计模式与“监听者”设计模式

发布于 2024-09-12 02:05:04 字数 189 浏览 5 评论 0原文

在我看来,GoF 中描述的观察者设计模式实际上与各种工具包中的监听器是一样的。这些概念之间是否存在差异,或者侦听器和观察者实际上是同一件事?

(我不是在寻找任何特定的计算机语言实现,我只是想从设计的角度了解差异(如果有的话)。是的,我知道SO上类似问题有几个答案,但它们是根深蒂固的关于特定语言的特定问题 - 我正在寻找设计答案,而不是语言答案。)

It seems to me that the Observer design pattern as described in GoF is really the same thing as Listeners found in various toolkits. Is there a difference between the concepts, or are Listeners and Observers really the same thing.

(I'm not looking for any specific computer language implementation, I just want to understand the difference (if any) from a design point of view. Yes, I know there are several answers to similar questions on SO, but they're rooted in specific questions about specific languages -- I'm looking for a design answer, not a language answer.)

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

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

发布评论

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

评论(3

逆光飞翔i 2024-09-19 02:05:04

术语“侦听器”是否指观察者模式将取决于上下文。例如,Java Swing 的“事件侦听器”是观察者模式实现的一部分,而 .Net“跟踪侦听器”则不是。

框架作者为参与给定模式实现的组件分配不同的名称并不罕见,但在讨论模式本身时通常使用官方模式名称。

关于设计,给定模式的实现通常会受到所使用的语言和平台的影响。因此,给定框架内观察者模式的特定实现(可能碰巧使用术语“监听器”来描述具体观察者的角色)可能与设计模式书中描述的略有不同。

Whether the term "listener" refers to the Observer pattern or not will depend upon the context. For example, Java Swing's "Event Listeners" are part of an Observer pattern implementation while .Net "Trace Listeners" are not.

It isn't uncommon for framework authors to assign different names to components participating in a given pattern implementation, but the official pattern names are generally used when discussing the patterns themselves.

Concerning design, the implementation of a given pattern will often be influenced by the language and platform being used. As such, a particular implementation of the Observer pattern within a given framework (which may happen to use the term "listener" to describe the role of the ConcreteObserver) might differ slightly from that described in the Design Patterns book.

舞袖。长 2024-09-19 02:05:04

Gamma 等人在《设计模式》中对观察者的描述具有双向性。等人。 (GoF)。

在他们对观察者的描述中,具体观察者之一可能会发出其主题发生变化的信号。主题持有所有具体观察者的列表,然后通知其列表。所有具体观察者,包括原动机,都会做出适当的反应。

监听器的常见实现似乎都对来自外部的事件做出反应。

所以,我想说监听器是观察者的一个不太通用的例子。

命名示例

如果您只是想知道使用什么名称,您可能希望使用“侦听器”来表示简单的内容,使用“观察者”来表示回调之外的任何复杂内容。

我的意思是字面上,一个人可以通过多种方式“观察”,但一个人只能“聆听”某些事物发出的外部噪音(例如回调、通知等)。

There's a two-way nature to the description of Observer in Design Patterns by Gamma et. al. (GoF).

In their description of Observer, one of the ConcreteObservers might signal a change to its Subject. The Subject, which holds a list of all ConcreteObservers, then notifies its list. All ConcreteObservers, including the prime mover, then react as appropriate.

The common implementations of Listeners seem to all react to events from outside.

So, I would say that the Listener is a less-generalized case of an Observer.

Naming example

If you just wonder what name to use, you probably want to use "listener" for simple stuff, and "observer" for anything complex that goes beyond callbacks.

I mean literally, a person can "observe" with many ways, but a person can only "listen" to the external noices which something makes (like callbacks, notifications and etc.)

私野 2024-09-19 02:05:04

监听器很可能是观察者模式的实现。侦听器本质上是在等待给定对象上发生事件,这正是观察者所做的事情。

我知道您并不是在寻求特定于语言的答案,但抽象地谈论这些东西有点困难。因此,如果我要在 .NET 中对此进行调查,我会倾向于在 .NET Reflector 中打开一个包含侦听器的程序集,这将允许我反汇编该程序集并根据设计模式检查其逻辑。

A listener may well be an implementation of the observer pattern. A listener is essentially waiting for an event to occur on a given object, which is what an observer does.

I know you're not after a language specific answer, but it's kind of hard to talk about this stuff in the abstract. So if I were to investigate this in .NET, I'd be inclined to open an assembly containing a listener in .NET Reflector, which will allow me to disassemble the assembly and check its logic against a design pattern.

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