问题描述
在我看来,GOF中所述的观察者设计模式确实与各种工具包中的听众相同.概念之间有区别,还是听众和观察者真的是同一件事.
(我不是在寻找任何特定的计算机语言实现,我只想从设计的角度了解差异(如果有).是的,我知道有关类似问题的答案,但是它们``植根于有关特定语言的特定问题 - 我正在寻找设计答案,而不是语言答案.)
推荐答案
术语"侦听器"是否指观察者模式是否取决于上下文.例如,Java Swing的"事件听众"是观察者模式实现的一部分,而.NET"跟踪侦听器"不是.
框架作者不少见
关于设计,给定模式的实现通常会受到所使用的语言和平台的影响.因此,在给定框架内的观察者模式的特定实现(可能碰巧使用"侦听器"来描述ConceteteObserver的作用)可能与设计模式书中描述的框架略有不同.
.其他推荐答案
Gamma等人在设计模式中对观察者的描述具有双向性质. al. (GOF).
在对观察者的描述中,其中一位混凝土生物可能会标志着其主题的变化.该主题保留了所有ConcreteObservers的列表,然后通知其列表.所有ConcreteObservers,包括Prime Mover,然后根据适当的反应.
听众的共同实现似乎都对外部事件做出了反应.
所以,我想说听众是观察者的剂量较少.
命名示例
如果您想知道要使用什么名字,则可能想将"侦听器"用于简单的东西,而"观察者"都超越了回调.
我的意思是,一个人可以通过多种方式"观察",但是一个人只能"聆听"某些事物(例如回调,通知等)的外部声音.
.其他推荐答案
听众很可能是观察者模式的实现.听众本质上正在等待事件发生在给定的对象上,这是观察者所做的.
我知道您不是在追求特定语言的答案,但是很难抽象地谈论这些东西.因此,如果我要在.NET中调查此问题,我将倾向于打开一个包含.NET反射器中的听众的组件,这将使我能够拆卸组件并根据设计模式检查其逻辑.
问题描述
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.)
推荐答案
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.
其他推荐答案
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.)
其他推荐答案
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.