We have previously seen about implementing Command Pattern using Java 8 Lambda expressions. Now, we would focus on implementing Observer Pattern using the Functional Programming Paradigm in Java 8.
Observer Pattern
The Problem Domain
The problem is taken from the book Java 8 Lambdas by Richard Warburton.
Let’s discuss about the problem before solving it using Java 8 Lambda Expression.
Nasa has decided to spy on the activities occurring on Moon. So, Nasa needs to build a module to check whether any asteroid or any apollo spacecraft has reached. Nasa needs an effective way so that they can change the module anytime according to their requirements. They might need to extend the module to more spying activities.
OOP way
First of all, I would love to tell you about how it can be achieved using the object oriented way.
Observer
Concrete Observer - Apollo
Concrete Observer - Asteroid
Client
Functional Way
Now let’s move on to the Functional Paradigm
Observer
Client
Benefits
Concrete Implementation of the observers have been passed as behavior which eventually made the application more concise and less bloated.