Rxjava 简明教程

RxJava - Overview

RxJava 是 ReactiveX 基于 Java 的扩展。它在 Java 中实现或 ReactiveX 项目。以下是 RxJava 的主要特性。

RxJava is a Java based extension of ReactiveX. It provides implementation or ReactiveX project in Java. Following are the key characteristics of RxJava.

  1. Extends the observer pattern.

  2. Support sequences of data/events.

  3. Provides operators to compose sequences together declaratively.

  4. Handles threading, synchronization, thread-safety and concurrent data structures internally.

What is ReactiveX?

ReactiveX 是一个项目,其目标是将响应式编程概念应用于不同的编程语言。响应式编程是指在数据出现时程序随之做出反应的方案。这是一种基于事件的编程概念,事件可以传播到注册观察者。

ReactiveX is a project which aims to provide reactive programming concept to various programming languages. Reactive Programming refers to the scenario where program reacts as and when data appears. It is a event based programming concept and events can propagate to registers observers.

根据 Reactive ,它们将观测者模式、迭代器模式和函数式模式的优点结合在一起。

As per the Reactive, they have combined the best of Observer pattern, Iterator pattern and functional pattern.

做正确的事情的观测者模式。ReactiveX 是观测者模式、迭代器模式和函数式编程的最佳思想的结合。

The Observer pattern done right. ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming.

Functional Programming

函数式编程围绕使用纯函数构建软件。纯函数不依赖于以前的状态,并且对于传递的相同参数始终返回相同的结果。纯函数有助于避免在多线程环境中常见与共享对象、可变数据和副作用相关的各种问题。

Functional programming revolves around building the software using pure functions. A pure function do not depends upon previous state and always returns the same result for the same parameters passed. Pure functions helps avoiding problems associated with shared objects, mutable data and side effects often prevalent in multi-threading environments.

Reactive Programming

响应式编程是指在数据流以异步方式出现时,将数据流进行处理的事件驱动编程。

Reactive programming refers to event driven programming where data streams comes in asynchronous fashion and get processed when they are arrived.

Functional Reactive Programming

RxJava 将这两个概念结合在一起,其中流的数据随着时间的推移而改变,消费者函数做出相应的反应。

RxJava implements both the concepts together, where data of streams changes over time and consumer function reacts accordingly.

The Reactive Manifesto

Reactive Manifesto 是一份在线文档,规定了应用软件系统的崇高标准。根据宣言,下列是有反应的软件的关键属性:

Reactive Manifesto is an on-line document stating the high standard of application software systems. As per the manifesto, following are the key attributes of a reactive software −

  1. Responsive − Should always respond in a timely fashion.

  2. Message Driven − Should use asynchronous message-passing between components so that they maintain loose coupling.

  3. Elastic − Should stay responsive even under high load.

  4. Resilient − Should stay responsive even if any component(s) fail.

Key components of RxJava

RxJava 有两个关键组件:可观察序列和观察者。

RxJava have two key components: Observables and Observer.

  1. Observable − It represents an object similar to Stream which can emit zero or more data, can send error message, whose speed can be controlled while emitting a set of data, can send finite as well as infinite data.

  2. Observer − It subscribes to Observable’s data of sequence and reacts per item of the observables. Observers are notified whenever Observable emits a data. An Observer handles data one by one.

如果没有出现项目或没有为之前的项目返回回调,则永远不会通知观察者。

An observer is never notified if items are not present or a callback is not returned for a previous item.