Other Tutorials

Observer Design Pattern In Java

Introduction:

In this tutorial, we’ll talk about the Observer design pattern.

There are two main aspects to the observer pattern – a Subject and the Observers. We use this pattern when our system has multiple objects, known as the observers, relying on the state of one particular object – the subject. All the observers register themselves to the subject. Whenever there is a change in subject’s state, all these observers get notified.

One real-world example of the usage of this observer pattern is the blog subscription. If you subscribe to the blog you like, you receive notifications as soon as it has any updates.

With this, let’s deep-dive on the concepts.

UML Representation:

We can represent the observer design pattern with the following UML:

Observer Design Pattern

Where we have:

  • the Subject: maintains a list of observers, provides methods to register/unregister observers. Also, has a notifyAll() method to notify all registered observers of any state change
  • SubjectImpl: the class extending the functionality of the Subject class, it holds a state object representing its current state. Note that it’s a good idea to have an immutable state object to prevent any unintentional updates by the observer
  • Observer: it’s an interface with an update() method which is invoked by the Subject to notify the observer of any changes in its current state
  • ConcreteObserver: these are the classes implementing the Observer interface, the observer objects register themselves to listen to a Subject

Blog Subscription Example:

Let’s say we want to notify our blog subscribers as soon as we publish a new post. Let’s implement it using the observer pattern.

To do so, we first define our Subject class:

The reason we have marked the Subject class as abstract is that we want a Subject to have at least some state.

Now that we have implemented our Subject superclass, let’s write our Blog class:

Our Blog class extends from Subject and invokes notifyAll() method within the post() method to notify all subscribers as soon as an article gets published.

Implementing Observer:

Let’s now define our Observer interface and the Subscriber class:

Note that a blog subscriber will use the subscribeTo() method to subscribe to a blog. Once the user is subscribed to a blog, he/she will automatically receive an update of any new posts published.

Testing Our Implementation:

Let’s quickly test out our code:

On executing the above code, we’ll have an output somewhat like:

Conclusion:

In this quick tutorial, we learned to implement the Observer design pattern. This design pattern is used to implement the publisher-subscriber JMS model. Also, classes like java.util.EventListener and javax.servlet.http.HttpSessionAttributeListener makes use of this pattern.

Moreover, it is good to know that Java provides basic implementations of the observer and the subject classes named as java.util.Observer and java.util.Observable respectively.

Be the First to comment.

Leave a Comment

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