Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Observer Pattern

Overview

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.

Pattern Structure

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.

Key Concepts

  • 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

Examples

ManualExample

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.

JavaBuiltInExample

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.

How to Compile

From the parent directory of ObserverPattern:

javac -d out ObserverPattern/ManualExample/*.java

Or for the Java built-in example (may not work on newer Java versions):

javac -d out ObserverPattern/JavaBuiltInExample/*.java

How to Run

java -cp out ObserverPattern.ManualExample.WeatherStation

Or for the Java built-in example:

java -cp out ObserverPattern.JavaBuiltInExample.WeatherStation

Benefits of the Observer Pattern

  • 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

Types of Observer Pattern

Push Model

  • 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

Pull Model

  • Subject sends minimal notification
  • Observers query the subject for specific data they need
  • Less coupling, more flexible

Real-World Examples

  • 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

When to Use

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

Important Notes

Java's Built-in Observer (Deprecated)

Java's java.util.Observable and java.util.Observer were deprecated in Java 9 and removed in later versions due to:

  • Observable being 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

Design Principles

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