- Forward


The Observer Pattern
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • The Problem:
    • Objects often need to communicate with each other (i.e., send messages to each other/invoke methods on each other)
    • Communication increases coupling
  • One Solution:
    • Change your perspective
A Trading System Example
Back SMYC Forward

A Bad Design

images/tick_architecture_bad.gif
The Problem with this Design
Back SMYC Forward
  • An Observation:
    • Every TickReader must have an associated TickWriter and TickerTape
  • The Problem:
    • This makes the TickReader much less re-usable
A Trading System Example (cont.)
Back SMYC Forward

A Better Design

images/tick_architecture_better.gif
Advantage of this Design
Back SMYC Forward
  • An Observation:
    • The TickReader plays a more passive role
  • The Advantage:
    • A TickReader need only have a list of (zero or more) TickListener objects that it will inform
Operationalizing this Observation
Back SMYC Forward
  • What's Needed?
    • A TickListener interface
  • An Important Question:
    • Can this specific example be generalized into a pattern?
The Observer Pattern
Back SMYC Forward
  • Intent:
    • Define a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified
  • Participants:
    • A subject
    • Some observers
The Observer Pattern (cont.)
Back SMYC Forward
images/observer1.gif
Issues Related to the Choice of Collection
Back SMYC Forward
  • Frequency of notifications vs. modifications (think about HashMap java.util.HashMap , ArrayList java.util.ArrayList , and LinkedList Java.util.LinkedList )
  • Need for concurrent notifications and modifications (think about CopyOnWriteArrayList java.util.concurrent.CopyOnWriteArrayList )
Other Terminology
Back SMYC Forward
  • Listener
  • Publish-Subscribe Pattern
A Complete Example
Back SMYC Forward
  • A Silly Text Processor:
    • Counts the number of words that start with an uppercase letter
    • Save the lines to a file
    • Shows the progress (e.g., the number of lines processed)
  • Some Observations:
    • This is not going to make us any money
    • We can use it to explore different designs
    • Java has an Observer java.util.Observer interface and an Observable java.util.Observable class that are not used in the following example so that you can see the details
A Complete Example (cont.)
Back SMYC Forward
An Implementation of the Worst Possible Design
javaexamples/observer/notcohesive/SillyTextProcessor.java
 
A Complete Example (cont.)
Back SMYC Forward

A Bad Design

images/observer-notcohesive.gif
A Complete Example (cont.)
Back SMYC Forward

A Better Design

images/observer-tightlycoupled.gif
A Complete Example (cont.)
Back SMYC Forward
  • This design is better
  • It is, however, too tightly coupled
A Complete Example (cont.)
Back SMYC Forward

Using the Observer Pattern

images/observer-good.gif
A Complete Example (cont.)
Back SMYC Forward

Implementing the Good Design

javaexamples/observer/LineObserver.java
 
A Complete Example (cont.)
Back SMYC Forward
javaexamples/observer/LineSubject.java
 
A Complete Example (cont.)
Back SMYC Forward
javaexamples/observer/LineReader.java
 
A Complete Example (cont.)
Back SMYC Forward
javaexamples/observer/SillyTextProcessor.java
 
A Complete Example (cont.)
Back SMYC Forward
javaexamples/observer/ProgressWindow.java
 
A Complete Example (cont.)
Back SMYC Forward
javaexamples/observer/UCWordCounter.java
 
A Complete Example (cont.)
Back SMYC Forward
javaexamples/observer/LineArchiver.java
 
There's Always More to Learn
Back -