The Observer Pattern seems very much like a Notifier Pattern, since it is based on the subject notifying objects that are interested. The "notify" part seems the most important, because without it, nothing gets notified.
观察者模式看起来非常像通告模式,因为它基于主题通知感兴趣的对象。 “通知”部分似乎是最重要的,因为没有它,没有任何通知。
(was there ever some thoughts to rename this pattern to the Notifier Pattern?)
(有没有想过将此模式重命名为通告模式?)
Are there any Observer Patterns out there that are more "observer based"?
是否存在更多“基于观察者”的观察者模式?
For example, one that has a timer so that the observers will look into the object being observed every n milliseconds.
例如,一个具有计时器的计数器,以便观察者每隔n毫秒观察被观察的对象。
Or one that (like in machine code), when a routine wants to observe a memory location, it calls a certain routine and says, "if memory location TEMPERATURE_VALUE (0x32FF2C12, for example) gets modified, then call me (it knows the value is modified by the hardware interrupt mechanism). In this case, the subject doesn't notify or cannot decide whether to notify or not, but is forced to be observed.
或者一个(比如在机器代码中),当例程想要观察内存位置时,它会调用某个例程并说“如果内存位置TEMPERATURE_VALUE(例如0x32FF2C12)被修改,那么请打电话给我(它知道值由硬件中断机制修改。在这种情况下,主体不会通知或不能决定是否通知,而是*观察。
2 个解决方案
#1
The observer pattern is so named because the objects attached to the subject object "observe" its behavior. That the mechanism for such observance (a push of information from the subject to the observer) is initiated by the subject object does not change the inherent function of the observer objects (namely, that of observing).
观察者模式之所以如此命名,是因为附加到主题对象的对象“观察”其行为。这种观察的机制(从主体向观察者推送信息)由主题对象启动,不会改变观察者对象的固有功能(即观察对象的固有功能)。
The pattern might be better described as a "subscriber pattern," in that the observer objects "subscribe" to events on the subject object, and thereafter "listen" to those events. It would be accurate to say that the observer object does not observe the subject object directly, but rather indirectly through the information provided to it through the subject object's firing of the subscribed events.
该模式可以更好地描述为“订户模式”,因为观察者对象“订阅”主题对象上的事件,然后“监听”那些事件。可以准确地说,观察者对象不直接观察主题对象,而是间接地通过主题对象触发订阅事件而提供给它的信息。
It wouldn't be the first time a pattern was misnamed. Inversion of Control sounds really complicated until you realize that all that really means is providing needed objects (dependencies) to a class by assigning them to parameters in a constructor. The term "Dependency Injection" was coined in an effort to make this concept clearer.
这不是第一次模式错误命名。控制反转声音真的很复杂,直到你意识到所有真正意味着通过将它们分配给构造函数中的参数来为类提供所需的对象(依赖)。为了使这个概念更加清晰,创造了“依赖注入”一词。
#2
After understanding the pattern more, I think this is the reason that it is called "The Observer Pattern":
在更多地理解了模式之后,我认为这就是它被称为“观察者模式”的原因:
1) any method can "notify". The data object (the subject) can notify 3 hard-coded objects, who are acting as observers. Or the data object can go through an dynamic array of "registered" observers and notify each of them. The key is not about notifying. The key is about registration.
1)任何方法都可以“通知”。数据对象(主题)可以通知3个充当观察者的硬编码对象。或者数据对象可以通过“已注册”观察者的动态数组并通知它们。关键不在于通知。关键是关于注册。
2) We don't want to hard-code the 3 observing objects, because this is tight-coupling and therefore decrease object-reuse. If we allow the "observer registration", then 2 objects can register and then observe the subject, or 3 objects can register and observe the subject, or any number of objects can register and observe the subject.
2)我们不想对3个观察对象进行硬编码,因为这是紧耦合,因此减少了对象重用。如果我们允许“观察者注册”,则2个对象可以注册然后观察主体,或者3个对象可以注册并观察主体,或者任何数量的对象可以注册和观察主体。
3) So, it is the "registration of the observers" that is the key idea of this pattern. So it can be called "The Observer Registration Pattern", or for short, just "The Observer Pattern".
3)因此,“观察者的注册”是这种模式的关键思想。所以它可以被称为“观察者注册模式”,或者简称为“观察者模式”。
#1
The observer pattern is so named because the objects attached to the subject object "observe" its behavior. That the mechanism for such observance (a push of information from the subject to the observer) is initiated by the subject object does not change the inherent function of the observer objects (namely, that of observing).
观察者模式之所以如此命名,是因为附加到主题对象的对象“观察”其行为。这种观察的机制(从主体向观察者推送信息)由主题对象启动,不会改变观察者对象的固有功能(即观察对象的固有功能)。
The pattern might be better described as a "subscriber pattern," in that the observer objects "subscribe" to events on the subject object, and thereafter "listen" to those events. It would be accurate to say that the observer object does not observe the subject object directly, but rather indirectly through the information provided to it through the subject object's firing of the subscribed events.
该模式可以更好地描述为“订户模式”,因为观察者对象“订阅”主题对象上的事件,然后“监听”那些事件。可以准确地说,观察者对象不直接观察主题对象,而是间接地通过主题对象触发订阅事件而提供给它的信息。
It wouldn't be the first time a pattern was misnamed. Inversion of Control sounds really complicated until you realize that all that really means is providing needed objects (dependencies) to a class by assigning them to parameters in a constructor. The term "Dependency Injection" was coined in an effort to make this concept clearer.
这不是第一次模式错误命名。控制反转声音真的很复杂,直到你意识到所有真正意味着通过将它们分配给构造函数中的参数来为类提供所需的对象(依赖)。为了使这个概念更加清晰,创造了“依赖注入”一词。
#2
After understanding the pattern more, I think this is the reason that it is called "The Observer Pattern":
在更多地理解了模式之后,我认为这就是它被称为“观察者模式”的原因:
1) any method can "notify". The data object (the subject) can notify 3 hard-coded objects, who are acting as observers. Or the data object can go through an dynamic array of "registered" observers and notify each of them. The key is not about notifying. The key is about registration.
1)任何方法都可以“通知”。数据对象(主题)可以通知3个充当观察者的硬编码对象。或者数据对象可以通过“已注册”观察者的动态数组并通知它们。关键不在于通知。关键是关于注册。
2) We don't want to hard-code the 3 observing objects, because this is tight-coupling and therefore decrease object-reuse. If we allow the "observer registration", then 2 objects can register and then observe the subject, or 3 objects can register and observe the subject, or any number of objects can register and observe the subject.
2)我们不想对3个观察对象进行硬编码,因为这是紧耦合,因此减少了对象重用。如果我们允许“观察者注册”,则2个对象可以注册然后观察主体,或者3个对象可以注册并观察主体,或者任何数量的对象可以注册和观察主体。
3) So, it is the "registration of the observers" that is the key idea of this pattern. So it can be called "The Observer Registration Pattern", or for short, just "The Observer Pattern".
3)因此,“观察者的注册”是这种模式的关键思想。所以它可以被称为“观察者注册模式”,或者简称为“观察者模式”。