Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

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

class MobileApp implements Observer {
    public void update(float temperature) {
        System.out.println("Mobile App: Temperature changed to " + temperature);
        // Perform specific behavior based on temperature update
    }
}
package ObserverPattern;

// Concrete Observers
class WeatherDisplay implements Observer {
    public void update(float temperature) {
        System.out.println("Weather Display: Temperature changed to " + temperature);
        // Perform specific behavior based on temperature update
    }
}
package ObserverPattern;
import java.util.List;
import java.util.ArrayList;

// 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);
        }
    }
}

Driver:

package ObserverPattern;

public class ObserverPatternDriver {
    public static void main(String[] args) {
        WeatherStation weatherStation = new WeatherStation();

        Observer weatherDisplay = new WeatherDisplay();
        Observer mobileApp = new MobileApp();

        weatherStation.addObserver(weatherDisplay);
        weatherStation.addObserver(mobileApp);

        // Simulate temperature change
        weatherStation.setTemperature(25.0f);
        System.out.println();

        // Simulate temperature change again
        weatherStation.setTemperature(30.0f);
        System.out.println();
        // Unregister an observer
        weatherStation.setTemperature(33.0f);
        weatherStation.removeObserver(mobileApp);
        System.out.println();
        System.out.println("Now we have removed the Mobile App Observer");
        weatherStation.setTemperature(33.0f);

    }
}

In the example above, the WeatherStation represents the subject (observable) that maintains a list of observers and provides methods to register, remove, and notify them. The Observer interface defines the update() method, which is implemented by concrete observers like WeatherDisplay and MobileApp. Each observer registers with the subject to receive updates.

When the state of the subject (temperature in this case) changes, the subject notifies all registered observers by calling their update() method. The observers receive the updated temperature and perform specific actions based on the temperature change.

By using the Observer pattern, the subject (WeatherStation) and observers (WeatherDisplay, MobileApp) are decoupled, allowing for flexible and modular designs. The subject doesn't need to know the specific details of its observers, and observers can easily be added or removed without affecting other parts of the system. This pattern is especially useful in scenarios where changes in one object need to be propagated to multiple other objects in a loosely coupled manner.

  • No labels