JavaIntermediate#design-patterns

What is the Observer pattern?

A behavioral pattern where an object (subject) maintains a list of dependents (observers) and notifies them automatically of state changes, commonly used for event-driven systems like GUI listeners or pub/sub systems.

Example
interface Observer { void update(String event); }
class EventBus {
  List<Observer> observers = new ArrayList<>();
  void notifyAll(String event) { observers.forEach(o -> o.update(event)); }
}

Related Questions

1
JavaIntermediate#design-patterns

What is the Strategy pattern?

Open
2
JavaAdvanced#design-patterns

What is the difference between the Adapter and Decorator patterns?

Open
3
JavaAdvanced#design-patterns#oop

What is the difference between composition and inheritance, and why is 'favor composition over inheritance' recommended?

Open