DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Trending

  • Moving 900+ GB of Prometheus Without a Cutover Weekend
  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems
  • To Switch, or Not to Switch the AI Model
  • How You Clear Your HTML5 Canvas Matters

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. The Observer Pattern in Java

The Observer Pattern in Java

Get an overview of the Java observer pattern using an inventory example.

By 
Roohi Agrawala
·
Jul. 16, 14 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
113.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Lizza decides to get a new dress for her 18th birthday party. She goes online to her favourite website fashionworld.com and selects a perfect Red dress. But oops its out of stock.

THE RED DRESS


She tries to find another dress but there is nothing that can replace it. So, she goes back to the dress, registers her email id and click on "Notify Me" button.

You must be wondering why am I telling you story of Red Dress? Reason is "Notify Me" button is an example of Observer Pattern.

Here is how "Notify Me" button works? As soon as the dress will be in stock she will be notified via an email.


Similarly, there could be other users waiting for the Red Dress. As shown below user1, user2 and user3 have registered for notification just like Lizza. So, they will be notified once dress is available.

User4 is not interested in Red Dress so, she never registered by clicking "Notify Me" button. No email will be sent to her when the dress is 'In Stock'

If user1 finds another dress and is no longer interested in the Red Dress. Then user1 can get the registration removed. In this case she will no longer be informed when dress is available.


Lets dive into Observer Pattern now.

  1. All the users who registered for notification in the above case are Observers (user1, user2, user3).
  2. The subject of their observation is Red Dress. So, Red Dress is Observable.

 There is one Red dress but many users interested in it.

Observer Pattern defines one to many relationship between Observable and observers

When the state of dress changes from 'Out of stock' to 'in stock' all users are notified.

When Observable changes state all Observers are notified

User4 is not registered hence he is not an observer in this case.

If User1 gets her registration removed later. She will not remain an Observer in that case and will not be notified.

Observers subscribe for an Observable. Observers can unsubscribe at any time

Time to write Java code to implement this pattern-

What functionalities will be required in an Observable?


  • Register observer
  • Remove observer
  • Notify observer on state change
Normal RedDress class will look like this-
To become observable RedDress need to implement functionalities of Observable (point 1 below).


RedDress maintains a list of all registered users or Observers (point 2 below)

A new user is added to the users list from addObserver() method (point 3 below)

user is removed from the list from removeObserver() method is called (point 4 below)

At any point of time RedDress will have the list of all the users who need to be notified.

'Observable maintains a list of Observers'

notifyObserver() method has responsibility of informing all the observers about the change.(point 5 below)

Any change to inStock will happen from the setter setInStock(). Whenever, this happens all the users need to be notified simply by calling notifyObserver method (point 6 below)

Now how will Observer look like?

Observable need to update all the Observers. So, Observer should have a method update() which Observable can call to notify about the change.

So, notifyObserver() method of Observable will call update() method of all Observers.

If a user has to become an Observer it will implement Observer functionality (Point 1 below).

From update method User can do something when state of Observable is changed. In this casebuyDress() is called. (Point 2 below)

Here, User is not doing much with the status of Observable. But in some cases user might need to know the changed current status. e.g. if user need to know the change in price of the dress. In such a case the state of Observable is passed as parameter to the update method.

update(int price);

Once, user calls buyDress() he is not interested in any status change of RedDress. So, it calls unsubscribe() to get removed from the observer list of RedDress (Point 3 below). 

For this removeObserver of Observable need to be called. User keeps a reference of Observable so that it can call removeObserver when required.

What we achieved?
We were able to separate the logic of Observer and Observable and make them independent of each other. Now user can has its own independent functionality e.g. goToWork(), driveCar() or any other functionality.
And any change to that will not impact our Observable. Vice versa is also true any change to Observable will not impact Observer.
 Due to use of Interfaces, Observable and Observers are loosely coupled.

In future if we decide that User should not implement Observer Interface no code change is required in Observable class as it uses Observer Interface and not the concrete class.

Similarly any new class can become an Observer by simply implementing Interface and it will not impact Observable.

Observer & Observable are loosely coupled. Change to one will not impact other

Based on this example lets create Class Diagram for above example-


Observer Pattern Class Diagram





Observer pattern Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook