Observer Design Pattern in Java

Observer Pattern is one of the behavioral patterns. Observer design pattern is useful when there is something need to be done with the state of an object and an object wants to get notified whenever there is any change in state. In observer pattern, the object that watches on the state of another object are called Observer and the object that is being watched is called Subject.

This pattern is useful when there is a one-to-many relationship between objects and when an object changes its state, all its dependent objects are notified and updated automatically.

Subject contains a list of observers to notify of any change in its state, so it should provide methods using which observers can register and unregister themselves. Subject also contains a method to notify all observers of any change in state and either it can send the update while notifying the observer or it can provide another method to get the update.

Observer should have a method to set the object to watch and another method that will be used by Subject to notify them of any update.

The definition provided in the original Gang of Four book is given below

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

The real life example of Observer Pattern is we want to publish recruitment status through three different medium such as PostCard, NewsPaper and Internet whenever an update or change occurs in the recruitment status.

The observer pattern is mainly used to implement distributed event handling systems. The Observer pattern is also a key part in the familiar model–view–controller (MVC) architectural pattern.

Class Diagram

Observer Pattern in Java
Implementation

Create an Observer interface which will update the state

Create a Subject interface for registering, unregistering and notifying observers

Create a class RecruitmentStatus which implements Subject.

Now we will broadcast the updated status through three different medium such as PostCard, NewsPaper and Internet whenever an update occurs in the status.

Create a test class for testing the Observer Pattern

Run the above test class and see the below output in the console

Output 

Usage

If you have an object that needs to share it’s state with others, without knowing who those objects are, the Observer is exactly what you need.

Advantages

The advantage of Observer Pattern is loose coupling between observer and observable objects. The subject only knows the list of observers but it doesn’t care about how observers are implemented. All observers are notified by subject in a single event.

Disadvantages

The disadvantage is debugging becomes very difficult because flow of control is implicitly between observers and observable and if chain of observers found (observers acting as subjects).
Another issue is memory management because subject holds all references of all observers, so if we do not unregister the object it can create the memory issue.

That’s all. Thanks for your reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.