观察者模式和反应式编程之间的区别是什么? [英] What is difference between observer pattern and reactive programming?

查看:66
本文介绍了观察者模式和反应式编程之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我听到了很多术语反应式编程.但是当我搜索它时,发现的只是与观察者模式的相似之处.实际上,我找不到它们之间的任何区别.它们之间在概念上有什么区别,以及为什么反应式编程一词引起人们的关注?

Recently I heard a lot the term reactive programming. But when I searched for it, what I discovered was only some similarities with observer pattern. Actually, I cannot find any different between them. What's conceptual difference between them and why the term reactive programming is getting buzzed?

推荐答案

响应式编程是通过执行程序轻松在数据流中传播更改的基本范例.它本身不是特定的模式或实体,它是编程的思想或风格(例如面向对象的编程,函数式编程等).松散地说,它是当x在一个位置更改或更新时的概念,依赖x的值将以无阻塞方式在其他各个位置重新计算和更新,而不必占用等待事件发生的线程.

Reactive programming is the general paradigm behind easily propagating changes in a data stream through the execution of a program. It's not a specific pattern or entity per-se, it's an idea, or style of programming (such as object oriented prorgamming, functional programming, etc.) Loosely speaking, it's the concept that when x changes or updates in one location, the things that depend on the value of x are recalculated and updated in various other locations in a non-blocking fashion, without having to tie up threads sitting around just waiting for events to happen.

传统上,您几乎总是看到上述模式,其中x是GUI事件.大多数GUI库都是单线程的,因此您不能占用这一线程来等待响应.这就是观察者模式的用处-它提供了一种提供触发"的通用方法,以允许在进行此类更改时(或者更常见的OO术语是在触发事件"时)更新信息.从这个意义上讲,它提供了一种简单的机制,用于使反应式编程的非常基本概念发生在OO(有时是其他)样式语言中.

Traditionally, you've near always seen the above pattern where x is a GUI event. Most GUI libraries are single threaded, so you can't tie up this one thread waiting for a response. That's where the observer pattern comes in - it provides a common method for providing a "trigger" to allow information to be updated whenever such a change is made (or, in more common OO terms, when an "event" is fired.) In that sense, it provides a simple mechanism for allowing the very basic concept of reactive programming to happen in OO (and sometimes other) style languages.

更全面的反应式编程概念超越了传统的观察者模式-不仅可以对单个事件(例如用户点击)触发特定动作,还可以创建并订阅此类事件的发布者根据该发布者上发生的事件运行的操作,施加反压以控制该发布者的速度,控制该流的线程等.

The fuller concept of reactive programming goes way, way beyond the traditional observer pattern - instead of just firing a particular action on a single event (such as a user click), you can create and subscribe to publishers of such events, set actions to run based on the events that occur on that publisher, apply backpressure to control the speed of that publisher, control the threading of that stream, etc.

这篇关于观察者模式和反应式编程之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆