Java observer example

Thomas 142 Published: 07/18/2024

Java observer example

I'm happy to help you with that!

The Observer design pattern is a behavioral pattern that defines a one-to-many dependency between objects, allowing changes to be notified to a list of dependent objects. It's commonly used for event-driven programming or when you want to decouple the sender of an event from its receivers.

Here's a simple example in Java:

// Subject (Observable) interface

interface Subject {

void attach(Observer observer);

void detach(Observer observer);

void notifyObservers();

}

// Observer interface

interface Observer {

void update(String message);

}

// Concrete subject class (WeatherStation)

class WeatherStation implements Subject {

private List observers;

private String temperature;

public WeatherStation() {

this.observers = new ArrayList<>();

}

@Override

public void attach(Observer observer) {

observers.add(observer);

}

@Override

public void detach(Observer observer) {

observers.remove(observer);

}

@Override

public void notifyObservers() {

for (Observer observer : observers) {

observer.update("Temperature is " + temperature + " degrees Celsius.");

}

}

public void setTemperature(String temperature) {

this.temperature = temperature;

// Notify observers when the temperature changes

notifyObservers();

}

}

// Concrete observer class (DisplayElement)

class DisplayElement implements Observer {

@Override

public void update(String message) {

System.out.println("Received update: " + message);

}

}

// Client code:

public static void main(String[] args) {

WeatherStation weatherStation = new WeatherStation();

DisplayElement displayElement1 = new DisplayElement();

DisplayElement displayElement2 = new DisplayElement();

// Attach observers to the weather station

weatherStation.attach(displayElement1);

weatherStation.attach(displayElement2);

// Change temperature, which triggers a notification

weatherStation.setTemperature("25 degrees Celsius.");

// Detach an observer

weatherStation.detach(displayElement1);

// Change temperature again, which still notifies other observers

weatherStation.setTemperature("30 degrees Celsius.");

}

In this example:

The WeatherStation class (a subject) maintains a list of attached Observers. When the temperature changes (setTemperature() method), it notifies all attached observers by calling their update() method and providing a message with the new temperature value. The DisplayElement class (an observer) simply prints out any updates it receives.

By using this pattern, you can decouple the weather station from its display elements, allowing for multiple displays to receive and respond to changes in the temperature without affecting each other or the weather station itself. This is a simple but effective way to manage complex dependencies between objects!

That's all for now! Do you have any questions about this example or the Observer design pattern in general?

Java observer tutorial

I'm happy to provide a Java Observer tutorial!

What is the Observer Pattern?

The Observer pattern is a behavioral design pattern that allows objects to notify other objects about changes to their state. It's commonly used when you need to decouple objects that interact with each other, making it easier to modify or extend your system without affecting the overall structure.

Problem: Decoupling Objects

Imagine you have a WeatherStation class that displays weather data (e.g., temperature, humidity). You also have a WeatherAlert class that sends notifications when certain conditions are met (e.g., extreme temperatures).

Initially, WeatherAlert might be tightly coupled with WeatherStation, making it difficult to modify or extend either class independently. For example, if you want to add a new type of weather alert, you'd have to modify the WeatherStation class as well.

Solution: Observer Pattern

The Observer pattern helps decouple these objects by introducing an intermediate layer, called the Observer, which is responsible for notifying interested parties (the observers) about changes to the subject's state.

Here's a step-by-step guide to implementing the Observer pattern in Java:

Define the Subject (WeatherStation): Create a Subject interface that has methods for registering and unregistering observers, as well as an update method to notify observers. Implement the Subject (e.g., WeatherStation class): Implement the Subject interface in your WeatherStation class. Add observers using the registerObserver() method. Remove observers using the unregisterObserver() method. Call the notifyObservers() method to update all registered observers when the subject's state changes. Define the Observer (WeatherAlert): Create an Observer interface that has a single method, update(), which will be called by the subject when its state changes. Implement the Observer (e.g., WeatherAlert class): Implement the Observer interface in your WeatherAlert class. Implement the update() method to handle the changed state and perform any necessary actions.

Here's a sample implementation:

// Subject (WeatherStation)

interface Subject {

void registerObserver(Observer observer);

void unregisterObserver(Observer observer);

void notifyObservers();

}

class WeatherStation implements Subject {

private List observers = new ArrayList<>();

@Override

public void registerObserver(Observer observer) {

observers.add(observer);

}

@Override

public void unregisterObserver(Observer observer) {

observers.remove(observer);

}

@Override

public void notifyObservers() {

for (Observer observer : observers) {

observer.update();

}

}

// Simulate weather data updates

public void updateWeather(float temperature, float humidity) {

// Update the weather station's state...

notifyObservers(); // Notify all registered observers about the change.

}

}

// Observer (WeatherAlert)

interface Observer {

void update();

}

class WeatherAlert implements Observer {

@Override

public void update() {

System.out.println("Extreme weather alert!");

}

// Simulate extreme weather condition detection

public boolean isExtremeCondition(float temperature, float humidity) {

return temperature > 30 && humidity < 0.2;

}

}

public class Main {

public static void main(String[] args) {

WeatherStation weatherStation = new WeatherStation();

WeatherAlert weatherAlert = new WeatherAlert();

weatherStation.registerObserver(weatherAlert);

weatherStation.updateWeather(35, 0.1); // Notify observers about the updated weather.

weatherStation.unregisterObserver(weatherAlert);

weatherStation.updateWeather(20, 0.3); // No notification is sent since the observer has been unregistered.

}

}

Benefits

The Observer pattern:

Decouples objects that interact with each other. Allows you to add or remove observers independently of the subject. Enables subjects and observers to communicate through a standardized interface (the Observer interface).

In this tutorial, we've learned how to implement the Observer pattern in Java, using a WeatherStation and WeatherAlert example. This pattern is essential for building scalable, maintainable systems that can adapt to changing requirements.

Hope you enjoyed this tutorial!