content: package org.alx.article._66_observer_pattern_in_java;
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  // Subject interface
  interface Subject {
      void registerObserver(Observer observer);
  
      void removeObserver(Observer observer);
  
      void notifyObservers();
  }
  
  // Concrete Subject
  class ConcreteSubject implements Subject {
      private List<Observer> observers;
      private int state;
  
      public ConcreteSubject() {
          this.observers = new ArrayList<>();
      }
  
      public int getState() {
          return state;
      }
  
      public void setState(int state) {
          this.state = state;
          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();
          }
      }
  }
  
  // Observer interface
  interface Observer {
      void update();
  }
  
  // Concrete Observer
  class ConcreteObserver implements Observer {
      private ConcreteSubject subject;
      private int observerState;
  
      public ConcreteObserver(ConcreteSubject subject) {
          this.subject = subject;
          this.subject.registerObserver(this);
      }
  
      @Override
      public void update() {
          this.observerState = subject.getState();
          display();
      }
  
      public void display() {
          System.out.println("Observer State: " + observerState);
      }
  }
  
  // Demo class to demonstrate Observer Pattern
  public class ObserverPatternDemo {
      public static void main(String[] args) {
          ConcreteSubject subject = new ConcreteSubject();
  
          ConcreteObserver observer1 = new ConcreteObserver(subject);
          ConcreteObserver observer2 = new ConcreteObserver(subject);
  
          subject.setState(10); // Updates all observers
          subject.setState(20); // Updates all observers
      }
  }