Observer Design Pattern
The Observer Design Pattern is a behavioral design pattern used in software engineering to establish a one-to-many dependency between objects. In this pattern, when the state of one object (called the subject) changes, all its dependent objects (called observers) are automatically notified and updated.
Key Components:
Subject: The object being observed. It maintains a list of observers and notifies them of any state changes.
Observer: The object that wants to be notified of changes in the subject’s state.
ConcreteSubject: A concrete implementation of the subject that holds the state and sends notifications when the state changes.
ConcreteObserver: A concrete implementation of an observer that reacts to changes in the subject’s state.
Step 1: Define the Observer Interface
interface Observer { void update(String state); }
Step 2: Define the Subject Interface
import java.util.ArrayList; import java.util.List; interface Subject { void attach(Observer observer); void detach(Observer observer); void notifyObservers(); }
Step 3: Implement the ConcreteSubject Class
class ConcreteSubject implements Subject { private Listobservers = new ArrayList<>(); private String state; @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(state); } } public String getState() { return state; } public void setState(String state) { this.state = state; notifyObservers(); // Notify observers when state changes } }
Step 4: Implement the ConcreteObserver Class
class ConcreteObserver implements Observer { private String name; public ConcreteObserver(String name) { this.name = name; } @Override public void update(String state) { System.out.println(name + " received state change: " + state); } }
Step 5: Usage Example (Main Method)
public class Main { public static void main(String[] args) { ConcreteSubject subject = new ConcreteSubject(); Observer observer1 = new ConcreteObserver("Observer 1"); Observer observer2 = new ConcreteObserver("Observer 2"); // Attach observers to the subject subject.attach(observer1); subject.attach(observer2); // Change state and notify observers subject.setState("New State 1"); // Detach one observer and change state again subject.detach(observer1); subject.setState("New State 2"); } }