Example for Observer Method Design Pattern in Java

Problem Statement

Let’s consider a simple weather monitoring system where we have a ‘WeatherStation’ as the subject, and various display devices (observers) like ‘CurrentConditionsDisplay’ and ‘StatisticsDisplay’ that want to be notified whenever the weather changes.

Explanation of Problem :

In this Problem:

  • ‘WeatherStation’ is the subject that maintains a list of observers and notifies them when the temperature changes.
  • ‘CurrentConditionsDisplay’ and ‘StatisticsDisplay’ are concrete observers that implement the Observer interface. They register themselves with the ‘WeatherStation’ and are notified whenever the temperature changes.
  • The ‘ObserverPatternExample’ class demonstrates how to use the Observer Pattern by creating a ‘WeatherStation’ and two display devices, registering the devices as observers, and simulating changes in temperature.

So this problem shows the flexibility of the Observer Pattern, allowing for a loosely coupled relationship between the subject and its observers.

Step Wise Implementation of above Problem:

Subject (Observable):

This is the object that maintains a list of observers and notifies us of any changes. In our case, it’s the ‘WeatherStation’ class.

Java




// Subject (Observable)
class WeatherStation {
    private List<Observer> observers = new ArrayList<>();
    private float temperature;
 
    public void addObserver(Observer observer) {
        observers.add(observer);
    }
 
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }
 
    public void setTemperature(float temperature) {
        this.temperature = temperature;
        notifyObservers();
    }
 
    private void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(temperature);
        }
    }
}


Observer:

This is the observer class which is implemented by concrete observer class. ‘ConcreteObserver’ is an implementation of the ‘Observer’ interface.

The ‘update’ method is called whenever the observed object’s state changes.

Java




// Observer
interface Observer {
    void update(float temperature);
}


Concrete Observer:

These are the classes that implement the Observer interface. In our case, ‘CurrentConditionsDisplay’ and ‘StatisticsDisplay’.

Java




// Concrete Observer
class CurrentConditionsDisplay implements Observer {
    private float temperature;
 
    @Override
    public void update(float temperature) {
        this.temperature = temperature;
        display();
    }
 
    private void display() {
        System.out.println("Current Conditions Display: Temperature = " + temperature);
    }
}
 
// Concrete Observer
class StatisticsDisplay implements Observer {
    private float temperature;
 
    @Override
    public void update(float temperature) {
        this.temperature = temperature;
        display();
    }
 
    private void display() {
        System.out.println("Statistics Display: Temperature = " + temperature);
    }
}


Main Program :

This main class is used to give inputs to the problem and called the observer methods declared above, in out case the main class is ‘ObserverPatternExample’.

Java




// Main class
public class ObserverPatternExample {
    public static void main(String[] args) {
        WeatherStation weatherStation = new WeatherStation();
 
        CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay();
        StatisticsDisplay statisticsDisplay = new StatisticsDisplay();
 
        weatherStation.addObserver(currentConditionsDisplay);
        weatherStation.addObserver(statisticsDisplay);
 
        // Simulate a change in temperature
        weatherStation.setTemperature(25.5f);
        // Output:
        // Current Conditions Display: Temperature = 25.5
        // Statistics Display: Temperature = 25.5
 
        // Simulate another change in temperature
        weatherStation.setTemperature(30.0f);
        // Output:
        // Current Conditions Display: Temperature = 30.0
        // Statistics Display: Temperature = 30.0
 
        // Remove an observer
        weatherStation.removeObserver(currentConditionsDisplay);
 
        // Simulate another change in temperature
        weatherStation.setTemperature(28.0f);
        // Output:
        // Statistics Display: Temperature = 28.0
    }
}


Overall Code Implementation of above Problem

Java




import java.util.ArrayList;
import java.util.List;
 
// Subject (Observable)
class WeatherStation {
    private List<Observer> observers = new ArrayList<>();
    private float temperature;
 
    public void addObserver(Observer observer) {
        observers.add(observer);
    }
 
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }
 
    public void setTemperature(float temperature) {
        this.temperature = temperature;
        notifyObservers();
    }
 
    private void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(temperature);
        }
    }
}
 
// Observer
interface Observer {
    void update(float temperature);
}
 
// Concrete Observer
class CurrentConditionsDisplay implements Observer {
    private float temperature;
 
    @Override
    public void update(float temperature) {
        this.temperature = temperature;
        display();
    }
 
    private void display() {
        System.out.println("Current Conditions Display: Temperature = " + temperature);
    }
}
 
// Concrete Observer
class StatisticsDisplay implements Observer {
    private float temperature;
 
    @Override
    public void update(float temperature) {
        this.temperature = temperature;
        display();
    }
 
    private void display() {
        System.out.println("Statistics Display: Temperature = " + temperature);
    }
}
 
// Main class
public class ObserverPatternExample {
    public static void main(String[] args) {
        WeatherStation weatherStation = new WeatherStation();
 
        CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay();
        StatisticsDisplay statisticsDisplay = new StatisticsDisplay();
 
        weatherStation.addObserver(currentConditionsDisplay);
        weatherStation.addObserver(statisticsDisplay);
 
        // Simulate a change in temperature
        weatherStation.setTemperature(25.5f);
        // Output:
        // Current Conditions Display: Temperature = 25.5
        // Statistics Display: Temperature = 25.5
 
        // Simulate another change in temperature
        weatherStation.setTemperature(30.0f);
        // Output:
        // Current Conditions Display: Temperature = 30.0
        // Statistics Display: Temperature = 30.0
 
        // Remove an observer
        weatherStation.removeObserver(currentConditionsDisplay);
 
        // Simulate another change in temperature
        weatherStation.setTemperature(28.0f);
        // Output:
        // Statistics Display: Temperature = 28.0
    }
}


Output

Current Conditions Display: Temperature = 25.5
Statistics Display: Temperature = 25.5
Current Conditions Display: Temperature = 30.0
Statistics Display: Temperature = 30.0
Statistics Display: Temperat...

Diagrammatic Representation of above Problem statement:

There are four main components of the observer method pattern are:

  • Subject (Observable)
  • Observer
  • Concrete Observer
  • Main Program

Working of observer Pattern

Observer Method Design Pattern in Java

Observer Design Pattern is a behavioral design pattern where an object, known as the subject, maintains a list of its dependents, called observers, that are notified of any changes in the subject’s state. This pattern is often used to implement distributed event handling systems.

Important Topics for Observer Method Design Pattern

  • How We can implement the Observer Method Design Pattern in Java?
  • Key Concepts of Observer Method Design Pattern
  • Example for Observer Method Design Pattern in Java
  • Use Cases of Observer Method Design Pattern
  • Advantage of Observer Method Design Pattern
  • Disadvantage of Observer Method Design Pattern

Similar Reads

How We can implement the Observer Method Design Pattern in Java?

Basically, in Java, the Observer pattern is implemented using the ‘java.util.Observer’ interface and the ‘java.util.Observable’ class. However, it’s important to note that the Observable class is considered somewhat outdated, and the ‘java.util’ package doesn’t provide a modern and flexible implementation of the Observer pattern....

Key Concepts of Observer Method Design Pattern

The key concepts of the Observer Pattern are:...

Example for Observer Method Design Pattern in Java

Problem Statement...

Use Cases of Observer Method Design Pattern

...

Advantage of Observer Method Design Pattern

...

Disadvantage of Observer Method Design Pattern

...