Observer Design Pattern

The observer pattern is a behavioral pattern and it expresses the dependency between objects. i.e one object and many dependent objects in such a way that when the state of one object changes all the dependent objects get notified. The objects which are watching the state change of the another object are called observers and that is being watched is called Subject.

The listener in Java which get notified when an event happens on user interface control is a classic example of Observer pattern.

So let’s take an example.

One day John and Abel decided to buy a watch online. But when searched for the same the specific model was not available. But there was an option in the online application to subscribe to get a notification when the respective stock arrives.

So below is the Subject interface which has methods for registering/ removing observers and also to notify observers.

public interface Subject {

public void registerObserver(Observer observer);

public void removeObserver(Observer observer);

public void notifyObservers();

}

And the Product class implements the Subject interface.

import java.util.ArrayList;

public class Product implements Subject {

private ArrayList<Observer> observers = new ArrayList<Observer>();

private String productName;

private String productType;

private boolean availability;

 

public Product(String productName, String productType, boolean availability) {

super();

this.productName = productName;

this.productType = productType;

this.availability = availability;

}

 

public String getProductName() {

return productName;

}

 

public void setProductName(String productName) {

this.productName = productName;

}

 

public String getProductType() {

return productType;

}

 

public void setProductType(String productType) {

this.productType = productType;

}

 

public boolean isAvailability() {

return availability;

}

 

public void setAvailability(boolean availability) {

this.availability = availability;

notifyObservers();

}

 

public ArrayList<Observer> getObservers() {

return observers;

}

 

public void setObservers(ArrayList<Observer> observers) {

this.observers = observers;

}

 

 

public void notifyObservers() {

for (Observer ob : observers) {

ob.update(this.availability);

}

}

 

public void registerObserver(Observer observer) {

observers.add(observer);

}

 

public void removeObserver(Observer observer) {

observers.remove(observer);

}

}

Following is the Observer interface having an update method.

public interface Observer {

public void update(boolean availability);

}

Person class is an implementation for Observer.

public class Person implements Observer {

String personName;

 

public Person(String personName) {

this.personName = personName;

}

 

public String getPersonName() {

return personName;

}

 

public void setPersonName(String personName) {

this.personName = personName;

}

 

public void update(boolean availabiliy) {

String status = (availabiliy) ? “available” : “not available”;

System.out.println(“Hello ” + personName + “, Product is now ” + status);

}

}

Now it’s time to test it.

public class ObserverPatternMain {

public static void main(String[] args) {

Person john = new Person(“John”);

Person abel = new Person(“Abel”);

Product fasTrackWatch = new Product(“fastrack”, “Watch”, false);

fasTrackWatch.registerObserver(john);

fasTrackWatch.registerObserver(abel);

fasTrackWatch.setAvailability(true);

}

}

Key Points :

Observer pattern handles a single source of events (i.e. a single publisher with multiple subscribers)

The intent of an observer pattern is to define a one-to-many dependency so that when one object (i.e. the publisher) changes its state, all its dependents (i.e. all its subscribers) are notified and updated correspondingly.

 

Jisha Oommen

 

About the author: Jisha Oommen is a Computer Science Engineering graduate from Mount Zion Institute of Science and Technology, Kozhuvalloor and currently doing Java/J2EE at jThread Trivandrum.

She can be reached at jishajov8@gmail.com

 

11 thoughts on “Observer Design Pattern

Leave a Reply

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