Overview

Observer Pattern is a behavioral type design pattern. According to GoF, the observer design pattern is;

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

An Object which is being watched by other objects is called Subject and Object which is watching state of the subject is called Observer.

In this pattern, There are many observers (objects) which are observing a subject (object). Observers are basically interested and want to be notified when there is a change made inside the subject. Hence, they register themselves with the subject. When they lose interest in the subject they simply unregistered from the subject. Sometimes this model is also referred to as the Publisher-Subscriber model.

Real-Life Example

Let’s take an example of a celebrity who has many fans. Each of these fans wants to get all the latest updates of his/her favorite celebrity. Hence, he/she can follow the celebrity as long as his/her interest persists. When he loses interest, he simply stops following that celebrity. Here fans are observers and celebrity is a subject.

Celebrity-Fan Example
Celebrity Fan Example

News Feed Example

Let’s implement observer design pattern with News Feed Example. Here viewers can register them-self to get updates on News and same way they can lose it if interest is no more persists, Viewers are acting as an Observer and News Channel will act as a Subject.

Based on the requirements of Subject, We are creating a blueprint of Subject interface having contract methods which need to be implemented by concrete Subject. In our example NewsChannel is a concrete Subject.

Next we will create a contract for the Observer, there will be a method to attach the Subject to the observer and another method to be used by Subject to notify of any change.

Now our subject and observer is ready, Let’s create concrete Subject[ie. NewsChannel] and Observer[ie. Viewers].

Method register/unregistered will add or remove the existing observer from the subject. We have also added extra method postNewsHeadLine(String newsHeadLine) to post new News on NewsChannel Subject.

Here is the implementation of Viewers, that will notify moment new News posted on NewsChannel Subject.

Here is a simple test program to test the implementation.

Output

Viewer 1:: No New News Found. New News Head-Line Available : Breaking News 1 Viewer 1:: New News :Breaking News 1 Viewer 2:: New News :Breaking News 1 Viewer 3:: New News :Breaking News 1

Observer Pattern Class Diagram

Observer Pattern Class Diagram
Observer Pattern Class Diagram

That’s all for Observer design pattern in java, Hope you liked it. Keep Learning and Sharing 🙂 !!

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

6 Thoughts on “Observer Design Pattern in Java”

Leave a Reply

Your email address will not be published. Required fields are marked *