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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs

Trending

  • How to Practice TDD With Kotlin
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Ethical AI in Agile
  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 user avatar
Roohi Agrawala
·
Jul. 16, 14 · Tutorial
Likes (27)
Comment
Save
Tweet
Share
112.8K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!