Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between observer pattern and reactive programming?

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

like image 520
eonil Avatar asked May 20 '13 15:05

eonil


People also ask

Is observer pattern reactive programming?

With reactive programming, it is assumed that there is a higher level of automation where the platform administers all the data and UI dependencies. So, as a general rule of thumb, is that if the observer pattern is used, then it is not a reactive system.

What is observer pattern in programming?

The observer pattern is a software design pattern in which an object, named 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.

What are observables in reactive programming?

An Observable is simply a collection of data that waits to be invoked (subscribed) before it can emit any data. If you've worked with promises, then the way to access the data is to chain it with the then() operator or use the ES6 async/await .

What is the difference between event and Observer?

If we stick with observer pattern, we say there is only one source of signal and it would be synchronous, while on other hand with events, we decouple both parties to work independently and the same time entertain the possibility of having more than one source of events in future without any code changes.


1 Answers

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 progamming, 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.

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.

like image 171
Michael Berry Avatar answered Oct 12 '22 10:10

Michael Berry