[Posts] On reactive programming (Reactive Programming)

On the reactive programming (Reactive Programming)

HTTPS: // www.jianshu.com/p/1765f658200a 

example of writing is very good.

 

0.9312018.02.14 21:22:16 1877 read 9816 words

This is the first time IT class blog written brief farewell CSDN, still adjusting. Is not the most suited markdown syntax notation direct hand lose. (The reason is that I do not like to switch Editor)

What is reactive programming (Reactive Programming)

In computing, reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change. This means that it becomes possible to express static (e.g. arrays) or dynamic (e.g. event emitters) data streams with ease via the employed programming language(s), and that an inferred dependency within the associated execution model exists, which facilitates the automatic propagation of the change involved with data flow.

-- Wikipedia

Wikipedia from the above explanation, in the computer field, a reactive programming is focused on the transfer of asynchronous data flow programming paradigm and variations. This means you can use the programming language is very easy for static (such as arrays) or dynamic (eg event emitter) data streams, and the associated execution model, there are inferred dependency, the existence of this relationship is conducive to automatic Communication and data flow related changes.

Put aside the concept of a large segment of the large segment, let's clear one thing: What is a programming paradigm?

Popular to say: programming is to solve problems, but to solve the problem can have a variety of perspectives and ideas, which is universal mode is attributed to the paradigm. We often say: "object-oriented", "process-oriented" is the programming paradigm.

Reactive programming model is a problem from the viewpoint of data flow and change. So to study reactive programming, we must bear in mind already know OO (object-oriented, I jump off everyone OO thinking is very deep-rooted a) to do the comparison, it must be put aside OO avoid a dead end.

Why is asynchronous?

Before the commencement of this issue, we look at a story, knowing almost lead: short story

Excerpt follows:

Aihe Cha Zhang, do not talk nonsense, boil water.

Dramatis personae: Zhang, kettle two (ordinary kettle, referred kettle; will ring the kettle, kettle abbreviated ring).

1 Zhang put the kettle on the fire, such as legislation to open water. (Synchronous blocking)
Zhang felt a little silly

2 Zhang put the kettle on the fire to the living room to watch TV from time to time to see if the kitchen is not open water. (Synchronous non-blocking)
Zhang still felt a bit silly, so change high-end, bought a kettle that will ring the flute. After the water is boiling, it can emit a loud beep ~ ~ ~ ~ noise.

3 Zhang put the kettle on the fire ring, legislation and other open water. (Asynchronous blocking)
Zhang felt so little significance Shadeng

4 Zhang put the kettle on the fire ring, to the living room watching TV, not to see it before the kettle rang, rang get another pot. (Asynchronous non-blocking)
Zhang feel smart.

The so-called synchronous asynchronous, but for the purposes of the kettle. Ordinary kettle, synchronization; whistling kettle, asynchronous. Although able to work, but the whistling kettle may be completed after themselves, prompting Zhang open water. This is a common kettle can not reach. Synchronization can only let the caller go to polling own (case 2), resulting in low efficiency of Zhang.

The so-called blocking non-blocking, only for Zhang terms. Zhang Li, etc., obstruction; Zhang watching TV, non-blocking. In cases 1 and 3 is blocked Zhang, his wife did not know he shouted. While 3 in response kettle is asynchronous, it may not make much sense for the erecting of Zhang. Therefore, the general is in line with asynchronous non-blocking use, so as to play an asynchronous effectiveness.

The above story is still a little problem, but the basic can explain the problem.

In response, the event must be one, a signal (like described) produced in the reaction. What response whistling kettle is it? The water temperature reaches a certain level, the reaction kettle will sound. Rang kettle, sound transmission to Zhang, Zhang reaction kettle is to go off.

Look at ordinary kettle, the temperature reaches a certain level, there is no reaction kettle, the reaction water bubbles in risk mist. But this signal is not easily transmitted to coming over to see, so Zhang in rotation only way to do things, not to go while waiting for notification.

For two kettle, the kettle is blocked, the water did not burn it can not do other things (for example, used to drop walnuts ???)

ok, back to our question: Why is asynchronous?

Return to nature to answer the question: reactive programming, is essentially a reaction to the data stream or make some kind of change, but when this change is unknown, so he is based asynchronous callback way in dealing with the problem .

Vicious circle: It seems that the vast majority of blog she spoke to began to explain RxAndroid

As the subtitle, search the Internet to the vast majority of the blog will she spoke to teach you how to use RxAndroid. Ladies and gentlemen, please remember the following:

  • RxAndroid (or RxJava) is very good reactive programming framework.

  • You do not necessarily need to use RxAndroid.

  • RxAndroid not like those blog that will make your code becomes more readable inside he said.

Here I go directly to the third point. Examples of access lines recommended RxJava was thrown in: the following code:

Observable.from(folders)
.flatMap((Func1) (folder) -> { Observable.from(file.listFiles()) }) .filter((Func1) (file) -> { file.getName().endsWith(".png") }) .map((Func1) (file) -> { getBitmapFromFile(file) }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe((Action1) (bitmap) -> { imageCollectorView.addImage(bitmap) }); 

On this code, the need to read it again before it can be from top to bottom will know his intentions?

Even for intensive reading the code, he might look like this:

Observable.from(folders) .flatMap(new Func1<File, Observable<File>>() { @Override public Observable<File> call(File file) { return Observable.from(file.listFiles()); } }) .filter(new Func1<File, Boolean>() { @Override public Boolean call(File file) { return file.getName().endsWith(".png"); } }) .map(new Func1<File, Bitmap>() { @Override public Bitmap call(File file) { return getBitmapFromFile(file); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Bitmap>() { @Override public void call(Bitmap bitmap) { imageCollectorView.addImage(bitmap); } }); 

ok, let me ask one more question, so the introduction of the code, you are going to use a single class to put it?

If you use the class to put a process similar to any business logic rx code segment, the number of classes may explode, and these names will look like very wonderful. If so, your business will not achieve class full of like Intensive not sure semantics easily be mistaken modified, it is not easy to test code. The face of such a code will only be skating on thin ice gingerly.

I was against the use of RxAndroid it?

No, I'm just against the abuse of Rx, Rx I agree with the use of certain highly abstract framework for building asynchronous behavior code is semantic in nature, such as: the preparation of MVVM hierarchical framework. Opposition to any business to do detail "Everything flows" no brain work. After all: business is gradually iterative development needs, for a test code support, but there is a strong semantic class, we can Extensive code "Hsien meaning" where the need for reconstruction code, modify Ho aware of the logic, without having to "flow" and then reverse back to the "real relationship", and then disrupt, modify, re-organized into a stream, then the next iteration nausea, and, most important thing is "you might want many find this a stream flow. "

 
 
 

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11691624.html