What is difference between observer pattern and reactive programming?

eonil picture eonil · May 20, 2013 · Viewed 12.8k times · Source

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?

Answer

Michael Berry picture Michael Berry · May 20, 2013

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.

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.