現(xiàn)在很多人都在討論響應式編程探熔,不少框架中都可以看到響應式編程的概念,比較好奇本谜,到底什么是響應式編程?
定義
常見的Reactive說法:
- 異步非阻塞編程
- 能夠提升系統(tǒng)性能
- 能解決傳統(tǒng)編程的困境
維基百科
In computing, reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. With this paradigm it is possible to express static (e.g., arrays) or dynamic (e.g., event emitters) data streams with ease, and also communicate that an inferred dependency within the associated execution model exists, which facilitates the automatic propagation of the changed data flow
在計算時傲醉,響應式編程是一種聲明式的編程范式連接數據流和傳播數據的變化,使用這種范式可以更加簡單的表達靜態(tài)(如數組)或動態(tài)(如事件發(fā)射器)的數據流呻率。
另外它能推斷執(zhí)行中的依賴關系硬毕,有助于數據流改變的時候能夠自動的傳播變化。
關鍵:
- 數據流(data streams)
- 傳播變化(propagation of change)
Reactive programming has been proposed as a way to simplify the creation of interactive user interfaces and near-real-time system animation
響應式編程已經被提議為一種簡化用戶交互接口和實時響應系統(tǒng)的方式礼仗。
https://en.wikipedia.org/wiki/Reactive_programming
Reactive宣言
Reactive Systems are:Responsive吐咳,Resilient,Elastic元践,Message Driver
響應式系統(tǒng)是:響應式的韭脊,彈性的,優(yōu)雅的单旁,消息驅動的沪羔。
- Responsive
- Resilient
- Elastic
- Message Driver
https://www.reactivemanifesto.org/
Spring定義
In plain terms reactive programming is about non-blocking applications that are asynchronous and event-driven and require a small number of threads to scale. A key aspect of that definition is the concept of backpressure which is a mechanism to ensure producers don’t overwhelm consumers. For example in a pipeline of reactive components that extends from the database to the HTTP socket when the HTTP client is slow the data repository slows down or stops until capacity frees up.
From a programming model perspective reactive programming involves a major shift from imperative style logic to a declarative composition of async logic. It is comparable to using
CompletableFuture
in Java 8 and composing follow-up actions via lambda expressions.
響應式編程是一種非阻塞的應用,它是異步和事件驅動的象浑,并且在擴展的時候需要很少量的線程蔫饰。背壓是其一個關鍵的概念肥哎,也是一種機制來確保生產者不會壓垮消費者睛蛛。
從編程模型的視角壹甥,響應式編程主要從表達式的寫法遷移到聲明式的寫法驰后。
關鍵點:
- 非阻塞應用
- 異步
- 事件驅動
- 擴展時需要線程少
- 背壓
Reactor定義
The reactive programming paradigm is often presented in object-oriented languages as an extension of the Observer design pattern. You can also compare the main reactive streams pattern with the familiar Iterator design pattern, as there is a duality to the
Iterable
-Iterator
pair in all of these libraries. One major difference is that, while an Iterator is pull-based, reactive streams are push-based.
Reactor是維基百科中對響應式編程的實現(xiàn),并且做了一些自己的理解状原。響應式編程是一種編程范式菩收,在面向對象編程中擴展了觀察者模式拨齐,也可以用于和迭代器模式做對比驰贷,與迭代器模式的主要區(qū)別是它是push數據摘盆,而迭代器是pull數據。
關鍵點:
- 維基百科定義的實現(xiàn)
- 擴展了觀察者模式
- push數據
https://projectreactor.io/docs/core/release/reference/#intro-reactive
ReactiveX定義
ReactiveX并沒有說自己是響應式編程饱苟,它利用了響應式編程的東西。
ReativeX: An API for asynchronous programming with observable streams
ReactiveX is more than an API, it's an idea and a breakthrough in programming. It has inspired several other APIs, frameworks, and even programming languages.
它不僅僅是一個API狼渊,它是一種理念和編程中的突破箱熬,影響了一些其它的API,框架狈邑,甚至編程語言城须。
至于實現(xiàn)上:則是擴展了觀察者模式,提供了操作符處理數據流米苹,聲明式的寫法糕伐。
http://reactivex.io/intro.html
最后
概念很多,但進行一次整合蘸嘶,大體概念:
響應式編程良瞧,異步陪汽,事件驅動,擴展觀察者模式褥蚯,擴容時消耗資源少挚冤,關注數據與數據的變化。
先這樣說吧...
參考:
https://medium.com/@kevalpatel2106/what-is-reactive-programming-da37c1611382