In this section, we will try to differentiate between the Observer pattern and Reactive pattern. As we have already discussed, the Reactive pattern gives a lot of benefits to our systems or applications.
The Observer pattern is a widely used OOP design pattern to solve some of the problems, and it mainly has two kinds of components—Subject and Object (where Subject is Observable and Object is Observer).
It gives us the following benefits:
- Separation of concerns into two distinct components—Subject and Object
- Clear abstraction and encapsulation between these two components
- Loose coupling between the Subject and Object components
- We can change a component without affecting others
- We can add more Objects or Observers at any time
Even though the Observer pattern solves most of the problems, it still has the following drawbacks or issues:
- It is not thread-safe
- It may cause leaking if we forget to unregister any Observers
- It does not support the backpressure technique
- It does not support composability, which means we cannot compose multiple small components to solve large or complex problems
- It does not support asynchronous non-blocking communication with backpressure
To solve all these problems, we should go for the Reactive Streams Specification.
The Reactive pattern is more than the Observer pattern. It is the combination of positive points from the Observer pattern, Iterator pattern, and FP.
Reactive pattern = Observer pattern + Iterator pattern + FP
The Reactive pattern or programming is not a single pattern; it is an architecture and gives us a new set of design patterns to develop new kinds of systems or applications (that is, Reactive systems or Reactive applications). We will discuss it in Chapter 12, Reactive Design Patterns and Best Practices.
By design, the Reactive pattern supports asynchronous non-blocking communication with backpressure. If we use Akka Reactive Streams or Play Framework with FP, we will get composability, thread-safety, concurrency, and parallelism for free.
Check out the Benefits of Reactive programming section for more details, which are also the same for the Reactive pattern.
Take a look at Chapter 7, Working with Reactive Streams, to understand backpressure.
Going forward, I hope my readers start thinking functional reactively.