努力理解观察者模式
我已经尽可能多地阅读了有关观察者模式的内容,但我无法完全理解它将使用什么/何时使用。谁能向我解释一下吗?
I've read as much as I can about observer patterns but I am failing to fully understand what/when it would be used. Can anyone explain this to me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将其视为发布者/订阅者模型。
一个例子是股票
当股票对象(主题)的价格更新时,任何订阅该股票的“投资者”(观察者)都会收到更新价格的通知
Think of it like a publisher / subscriber model.
An example would be stocks
When the price of a stock object (subject) is updated, any "investor" (observer) that is subscribed to this stock is notified of the updated price
观察者用于您希望在某些内容发生更改时通知代码的不同组件的情况。我想到了基于事件的系统。通常在基于事件的系统中,当您注册处理程序时,无论您使用什么框架,都将使用观察者实现来在事件触发时通知处理程序。
我见过它使用的一个领域是 Sproutcore 框架。该框架有一个称为“键值观察”的功能。基本上,您可以在对象的字段上设置观察者,当该字段值发生变化时,观察者会自动触发。这是有利的,因为例如,如果视图元素在父视图的布局上有一个观察者,那么当调整大小时,视图可以重新绘制自身。或者,假设一个值发生变化,您需要重新计算其他一些值,您可以在初始值上设置一个观察者,然后您可以从观察者中进行重新计算。
请参阅http://en.wikipedia.org/wiki/Observer_pattern
Observer is used in situations where you want disparate components of the code to be notified when something changes. Event based systems come to mind. Typically in event based systems, when you register the handler, whatever framework you use will use an observer implementation to notify the handlers when the event fires.
One area I have seen it used is in the Sproutcore framework. The framework has a feature called 'key-value observing'. Basically, you can set up observers on a field of an object, and when that field value changes, the observers fire automatically. This is advantageous because, for example, if a view element has an observer on a parent view's layout, when things get resized the view can redraw itself. Or, lets say a value changes and you need to recalculate some other values, you set up an observer on the initial value and then from your observer you can do the recalculation.
See http://en.wikipedia.org/wiki/Observer_pattern
我想一个例子可以清楚地说明这一点:假设您正在从套接字接收数据,并且希望在某些数据到达时收到通知。因此,您可以向接收数据的类注册一个观察者。数据到达后,将调用观察者的特定方法,您可以在其中采取适当的操作来处理数据。
I guess an example will make it clear: Let's say you are receiving data from a socket and want to be notified whenever some data arrives. So you register a observer with the class that is receiving the data. Once the data comes, a particular method of your observer will be called in which you can appropriate action to process the data.
当许多观察者需要了解某个对象的状态变化时,可以使用观察者模式。例如,它可以用在 GUI 中,当其他组件更改时,您希望多个组件“自动”更改。
假设您有一个复选框,并且根据状态(选中或未选中)您希望其他组件可见或不可见。其他组件的数量可以是可变的。所以你希望他们都通过复选框注册自己。然后,该复选框会通知每个观察者,当用户单击时发生了更改,他们可以决定如何对该更改做出反应。因此,他们如何反应的逻辑在于观察者。复选框不需要知道它们的作用。
The observer pattern is used when many observers need to know about state changes in some object. It can be used for instance in a GUI where you want several components to change "automatically" when some other component changes.
So let's say you have a check box and depending on the state (checked or unchecked) you want other components to be visible or not. The number of other components may be variable. So you want them all the register themselves with the checkbox. The checkbox then notifies each observer that a change has occurred when the user clicks and they can decide how to react to that change. So the logic of how they react resides in the observers. The checkbox does not need to know what they do.
这是关于此的好文章:
Here is nice article on this: