反应式编程中的 "observable" 是什么?
What is an "observable" in reactive programming?
Microsoft says, "developers represent asynchronous data streams with Observables." 我正在尝试通过这个想法进行推理。如果我要隐含地处理这个概念,我会想象它只是,可以在数据流中观察到的任何东西。 代码应该更精确。
如果我看到了 "observable",我怎么知道它?你能更好地解释一下 "observable" 是什么吗?
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
当应用于用户界面中使用的事件时,此定义很清楚:您通过提供事件处理程序来观察按钮点击,按钮在单击时调用该事件处理程序。在这种情况下,按钮是一个可观察的对象,它以事件处理程序的形式通知多个观察者。
应用于响应式编程,可观察对象只是您可以订阅的事件流 - 即观察。将它想象成一个管道,事件通过它传递,您可以窥视它。您可以通过观察流并处理您感兴趣的事件来实现。此外,可以对流执行操作 - 例如将几个流合并为一个新流。
将事件发布到流和处理这些事件(处理它们的观察者)都可以异步完成,从而提高可伸缩性。
类似的概念是消息、主题和订阅者:一些利益相关者可以将消息发布到一个主题,许多不同的利益相关者可以订阅该主题。这些分别对应于事件、可观察流事件和观察者。
Microsoft 使用术语 Observer 和 Observable 而在其他一些反应式框架中他们可能使用其他术语。 Getting started of Introduction to Rx可以帮助你进一步理清这些概念,全书免费gem。请注意,本书更喜欢使用术语 序列 来指代事件流。
I would imagine that it's just, anything that could be observed in the data stream.
没错。实际上,在微软的Rx
中,主要核心只是定义观察者和可观察对象之间的契约的两个接口接口,其余的都被抽象掉了。
我认为术语各不相同,但如果您搜索功能反应式编程论文,例如在 Google Scholar 上,您会找到基本概念行为和事件的定义。我认为Functional Reactive Programming from First Principles一篇论文中的以下两个定义具有代表性:
Behavior is a value of type a that changes over time
Event is a
time-ordered sequence of event occurences
Intuitively, a behavior is a stream transformer: a function that takes
an infinite stream of sample times, and yields an infinite stream of
values. Similarly, an event is a stream transformer, and can be
thought of as a behavior where, at each time t, the event either
occurs or does not occur.
MS 似乎将两者融合到 Observable 的概念中。
我认为阅读一些背景论文以了解术语是很好的。如果您想要更具互动性的介绍,请访问 coursera 的 papers from Conal Elliott are a good start. Or you could enlist in the Principles of Reactive Programming。
Microsoft says, "developers represent asynchronous data streams with
Observables." I'm trying to reason through the idea. If I were to
tackle the concept implicitly, I would imagine that it's just,
anything that could be observed in the data stream. Code should be
more precise.
代码实际上更精确。 Observable 由 IObservable<T>
interface. The main job of IObservable<T>
is to handle IObserver<T>
表示。这两个协同工作:IObservable<T>
表示可以订阅的 T
类型的流。 IObserver<T>
表示订阅可观察对象以处理这些事件的处理程序。
Observable 可以隐式发出三种类型的事件:
- OnNext:
T
的下一个实例
- OnCompleted:非错误(空消息)终止符。
- OnError:错误终止符。
但是,可观察对象不会直接发出这些消息,而是只会将它们发送给订阅的观察者。
How would I know an "observable" if I saw it? Could you give me a
better explanation of what an "observable" is?
想象一下报告最新 Apple 股票价格的服务。您可以将服务视为可观察的。要获得此信息,您必须订阅该服务。订阅后,该服务可以发出以下三种消息之一:
- 次最新股价
- 市场关闭
- 某种失败(连接失败是最典型的)
您将依次编写处理程序来处理这三种类型的消息。该处理程序将是可观察的价格流的观察者。
Microsoft says, "developers represent asynchronous data streams with Observables." 我正在尝试通过这个想法进行推理。如果我要隐含地处理这个概念,我会想象它只是,可以在数据流中观察到的任何东西。 代码应该更精确。
如果我看到了 "observable",我怎么知道它?你能更好地解释一下 "observable" 是什么吗?
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
当应用于用户界面中使用的事件时,此定义很清楚:您通过提供事件处理程序来观察按钮点击,按钮在单击时调用该事件处理程序。在这种情况下,按钮是一个可观察的对象,它以事件处理程序的形式通知多个观察者。
应用于响应式编程,可观察对象只是您可以订阅的事件流 - 即观察。将它想象成一个管道,事件通过它传递,您可以窥视它。您可以通过观察流并处理您感兴趣的事件来实现。此外,可以对流执行操作 - 例如将几个流合并为一个新流。
将事件发布到流和处理这些事件(处理它们的观察者)都可以异步完成,从而提高可伸缩性。
类似的概念是消息、主题和订阅者:一些利益相关者可以将消息发布到一个主题,许多不同的利益相关者可以订阅该主题。这些分别对应于事件、可观察流事件和观察者。
Microsoft 使用术语 Observer 和 Observable 而在其他一些反应式框架中他们可能使用其他术语。 Getting started of Introduction to Rx可以帮助你进一步理清这些概念,全书免费gem。请注意,本书更喜欢使用术语 序列 来指代事件流。
I would imagine that it's just, anything that could be observed in the data stream.
没错。实际上,在微软的Rx
中,主要核心只是定义观察者和可观察对象之间的契约的两个接口接口,其余的都被抽象掉了。
我认为术语各不相同,但如果您搜索功能反应式编程论文,例如在 Google Scholar 上,您会找到基本概念行为和事件的定义。我认为Functional Reactive Programming from First Principles一篇论文中的以下两个定义具有代表性:
Behavior is a value of type a that changes over time
Event is a time-ordered sequence of event occurences
Intuitively, a behavior is a stream transformer: a function that takes an infinite stream of sample times, and yields an infinite stream of values. Similarly, an event is a stream transformer, and can be thought of as a behavior where, at each time t, the event either occurs or does not occur.
MS 似乎将两者融合到 Observable 的概念中。
我认为阅读一些背景论文以了解术语是很好的。如果您想要更具互动性的介绍,请访问 coursera 的 papers from Conal Elliott are a good start. Or you could enlist in the Principles of Reactive Programming。
Microsoft says, "developers represent asynchronous data streams with Observables." I'm trying to reason through the idea. If I were to tackle the concept implicitly, I would imagine that it's just, anything that could be observed in the data stream. Code should be more precise.
代码实际上更精确。 Observable 由 IObservable<T>
interface. The main job of IObservable<T>
is to handle IObserver<T>
表示。这两个协同工作:IObservable<T>
表示可以订阅的 T
类型的流。 IObserver<T>
表示订阅可观察对象以处理这些事件的处理程序。
Observable 可以隐式发出三种类型的事件:
- OnNext:
T
的下一个实例
- OnCompleted:非错误(空消息)终止符。
- OnError:错误终止符。
但是,可观察对象不会直接发出这些消息,而是只会将它们发送给订阅的观察者。
How would I know an "observable" if I saw it? Could you give me a better explanation of what an "observable" is?
想象一下报告最新 Apple 股票价格的服务。您可以将服务视为可观察的。要获得此信息,您必须订阅该服务。订阅后,该服务可以发出以下三种消息之一:
- 次最新股价
- 市场关闭
- 某种失败(连接失败是最典型的)
您将依次编写处理程序来处理这三种类型的消息。该处理程序将是可观察的价格流的观察者。