Article image
Weslley Prado
Weslley Prado27/04/2023 09:43
Compartilhe

Observer Design Pattern

  • #Java

The Observer pattern is a design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

Advantages of Observer Pattern

  • Allows loose coupling between objects: The subject only needs to know that an observer has been added, it doesn’t need to know anything else about the observer.
  • Support for broadcast communication: When the subject changes, all registered observers are notified and updated automatically.
  • Can be used to implement event handling systems.

Example Implementation

Here is a simple example of the Observer pattern in Java:

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


interface Subject {
  void registerObserver(Observer observer);
  void removeObserver(Observer observer);
  void notifyObservers();
}


class WeatherStation implements Subject {
  private List<Observer> observers = new ArrayList<>();
  private int temperature;


  public void setTemperature(int temperature) {
      this.temperature = temperature;
      notifyObservers();
  }


  @Override
  public void registerObserver(Observer observer) {
      observers.add(observer);
  }


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


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


interface Observer {
  void update(int temperature);
}


class PhoneDisplay implements Observer {
  private int temperature;


  @Override
  public void update(int temperature) {
      this.temperature = temperature;
      System.out.println("The current temperature is " + temperature + " degrees Celsius");
  }
}


class TVDisplay implements Observer {
  private int temperature;


  @Override
  public void update(int temperature) {
      this.temperature = temperature;
      System.out.println("The current temperature is " + temperature + " degrees Fahrenheit");
  }
}


public class Main {
  public static void main(String[] args) {
      WeatherStation weatherStation = new WeatherStation();
      PhoneDisplay phoneDisplay = new PhoneDisplay();
      TVDisplay tvDisplay = new TVDisplay();


      weatherStation.registerObserver(phoneDisplay);
      weatherStation.registerObserver(tvDisplay);


      weatherStation.setTemperature(20);
      weatherStation.setTemperature(68);


      weatherStation.removeObserver(tvDisplay);


      weatherStation.setTemperature(25);
  }
}


In this example, the WeatherStation class is the subject, and PhoneDisplay and TVDisplay are the observers. When the WeatherStation's temperature changes, it calls the `notifyObservers()` method, which calls the `update()` method on each observer. The observers then update their temperature display accordingly.

Conclusion

The Observer pattern is a powerful design pattern that allows objects to communicate with each other in a loosely coupled way. By using this pattern, you can create flexible, modular, and reusable code that is easy to maintain and extend.

Compartilhe
Comentários (0)