Observer Pattern in Java

The Observer pattern defines one to many relationship between a group of objects so that when one object changes state, all of its dependents are notified.
 
observer pattern
 
There two types of actors here :

“Subject and Observers”

The Subject manages some sort of data and the Observers should be updated when the data changes.

 

Create Subject interface

 
For implementing the Subject, we need to provide the following functionalities :
 
– Register observer
– Remove observer
– Notify observer on state change

 

package com.topjavatutorial.patterns;

public interface Subject {

  void addObserver(Observer ob);
  void removeObserver(Observer ob);
  void notifyObservers();
}

 

Create Observer interface

 
Observers need to be updated about change from the Subject. For this, lets create an Observer interface and add a update() method in it.
 
Here, we want to be notified of the color of our subject changes.
 

package com.topjavatutorial.patterns;

public interface Observer {
  void update(String color);
}

 

Create Subject implementation

 
Lets provide an implementation for the Subject.
 
We will create a ColorfulSubject that implements Subject.
 

package com.topjavatutorial.patterns;

import java.util.ArrayList;
import java.util.List;

public class ColorfulSubject implements Subject {

  private List<Observer> observers;
  
  private String colorMonitored;

  public ColorfulSubject() {
    observers = new ArrayList<Observer>();
  }

  @Override
  public void addObserver(Observer ob) {
    observers.add(ob);

  }

  @Override
  public void removeObserver(Observer ob) {
    observers.remove(ob);
  }

  @Override
  public void notifyObservers() {
    for (Observer observer : observers)
      observer.update(colorMonitored);
  }

  public String getColorMonitored() {
    return colorMonitored;
  }

  public void setColorMonitored(String colorMonitored) {
    this.colorMonitored = colorMonitored;
    notifyObservers();
  }
}

 
Note the setter method for colorMonitored carefully. We are calling notifyObservers() so that all observers are notified.
 

Create Observer implementations

 
Lets add some Observers that implement the Observer interface we created above.
 

ObserverOne

 

package com.topjavatutorial.patterns;

public class ObserverOne implements Observer {

  @Override
  public void update(String color) {
    System.out.println("ObserverOne notified and updated for color " + color);
  }

}


 

ObserverTwo

 

package com.topjavatutorial.patterns;

public class ObserverTwo implements Observer {

  @Override
  public void update(String color) {
    System.out.println("ObserverTwo notified and updated for color " + color);
  }

}


 
Now, we are ready with the implementation for the Observer pattern.
 

Testing the Observer pattern implementation

 
To test our implementation, lets create an ObserverPatternDemo class:
 

package com.topjavatutorial.patterns;

public class ObserverPatternDemo {

  public static void main(String[] args) {
    ColorfulSubject subject = new ColorfulSubject();
    
    ObserverOne ob1 = new ObserverOne();
    ObserverTwo ob2 = new ObserverTwo();
    
    subject.addObserver(ob1);
    subject.addObserver(ob2);
    
    subject.setColorMonitored("Red");
  }

}


 
Running this class will create following output :
 

Output :

 
ObserverOne notified and updated for color Red
ObserverTwo notified and updated for color Red
 

Loose coupling between Subjects and Observers

 
In Observer design pattern, the Subjects and Observers are loosely coupled.
 
Because of this, changes to subjects and observers are independent of each other.
 
Also, we can add or remove observers any time without impacting the Subjects.
 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags