The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
The Observer Pattern establishes a relationship between a subject (publisher) and multiple observers (subscribers). When the subject's state changes, it automatically notifies all registered observers.
- Subject: The object that holds state and notifies observers of changes
- Observer: The interface that objects implement to receive updates from the subject
- ConcreteSubject: The actual object that holds state and sends notifications
- ConcreteObserver: Implements the Observer interface and maintains a reference to the subject
A weather station implementation using a custom Observer pattern. Demonstrates how to build the pattern from scratch with a Subject interface and Observer interface.
See the ManualExample README for details.
Uses Java's built-in java.util.Observable and java.util.Observer classes (deprecated in Java 9+). Shows how to use the language's built-in support for the Observer pattern.
See the JavaBuiltInExample README for details.
Note: This example won't work on newer versions of Java because Observable and Observer were deprecated in Java 9 and removed in later versions.
From the parent directory of ObserverPattern:
javac -d out ObserverPattern/ManualExample/*.javaOr for the Java built-in example (may not work on newer Java versions):
javac -d out ObserverPattern/JavaBuiltInExample/*.javajava -cp out ObserverPattern.ManualExample.WeatherStationOr for the Java built-in example:
java -cp out ObserverPattern.JavaBuiltInExample.WeatherStation- Loose Coupling: Subject and observers are loosely coupled
- Flexibility: Observers can be added or removed at runtime
- Broadcast Communication: Subject doesn't need to know about specific observers
- Reusability: Subject and Observer can be reused independently
- Open/Closed Principle: Can add new observers without modifying the subject
- Subject sends detailed data to observers (used in the weather station examples)
- Observers receive all data whether they need it or not
- More coupling between subject and observers
- Subject sends minimal notification
- Observers query the subject for specific data they need
- Less coupling, more flexible
- Event Listeners: GUI components (buttons, text fields) and their listeners
- Model-View-Controller: Model is subject, views are observers
- Messaging Systems: Publish-subscribe systems
- Reactive Programming: RxJava, React, and other reactive frameworks
Use the Observer Pattern when:
- A change to one object requires changing others, and you don't know how many objects need to be changed
- An object should be able to notify other objects without making assumptions about who those objects are
- You want to establish a one-to-many dependency between objects
- You need to decouple the subject from its observers
Java's java.util.Observable and java.util.Observer were deprecated in Java 9 and removed in later versions due to:
Observablebeing a class (not an interface), limiting flexibility- Thread-safety issues
- Not supporting lambda expressions well
Modern alternatives:
- Java's PropertyChangeListener and PropertyChangeSupport
- Reactive Streams (java.util.concurrent.Flow)
- Third-party libraries like RxJava
The Observer Pattern demonstrates:
- Strive for loosely coupled designs: Objects should interact with minimal knowledge of each other
- Program to interfaces, not implementations: Observers implement an interface
- Favor composition over inheritance: Subjects maintain a list of observers