Observer Design Pattern in Java

By | July 6, 2020

In this post, We will talk and learn about the Observer Design Pattern in Java.

Key Points About Observer Design Pattern :

  • Observer Patternfalls under behavioral design pattern.
  • An observer design pattern is very useful when you are interested in the state of an object and want to get notified whenever there is any change or update.
  • In an observer pattern, the object that keeps watch on the state of another object are called the object that is being watched is called Subject.
  • In this Pattern, we define a one-to-many relationship or dependency between objects so that when one object changes state than all its dependent objects are notified and updated automatically.
  • The subject has a list of observers to notify of any change in its state, so it provides a method using which observers can register and unregister themselves. The subject also contains a method to notify all the observers of any change or update and either it can send the update while notifying the observer or it can provide another method to get the update.

Observer Design Pattern in JDK & J2EE:

Now Let’s move towards the implementation of the Observer Design Pattern.

Below is the complete source code:

Observer Design Pattern

Output of this Program: 

Message for first subscriber :: This is First Message
Message for Second subscriber :: This is First Message
Message for Third subscriber :: This is Second Message
Message for Second subscriber :: This is Second Message

You May Also Like:

Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
Facade Design Pattern in Java
Bridge Design Pattern in Java.
Mediator Design Pattern in Java
Strategy Design Pattern in java

That’s all about the Observer Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

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