Design Patterns: The Observer Pattern in Java
by Riley MacDonald, August 7, 2017

The Observer Pattern utilizes a one to many relationship to notify interested objects of changes. Observers register with a Subject to be notified of updates. Observers with the new data. Java has it’s own implementation of the Observer pattern but it has limitations:

  • Observable is a class which must be subclassed. This restricts objects which are already subclassed from being candidates for the Observable pattern.
  • No Observable interface exists so you can’t create your own implementation.

To get around these limitations and implement the Observer Pattern let’s start by rolling our own set of interfaces. This example uses Newspapers as an example. People, apartment buildings and businesses (for example) can all subscribe to a newspaper to receive the latest publications.

Subject:
The Subject in this case would be the newspaper company. It’s responsible for notifying all the interested objects that a new publication is ready.

public interface NewspaperSubject {
    void registerObserver(Observer observer);
    void unregisterObserver(Observer observer);
    void notifyObservers();
}

Observer:
The Observer in this case would be anything interested in receiving a newspaper when one is published.

public interface NewspaperObserver {
    void update(Newspaper newspaper);
}

Concrete Implementation – Subject:
For brevity the publishListener() method is invoked when a new publication is published.

public class DailyNews implements NewspaperSubject {
    public List<Observer> observerList;
    public Newspaper newspaper;
 
    public NewspaperSubject() {
        observerList = new ArrayList<Observer>();
    }
 
    public void publishListener(Newspaper newspaper) {
        this.newspaper = newspaper;
        notifyObservers();
    }
 
    @Override
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }
 
    @Override
    public void unregisterObserver(Observer observer) {
        int index = observerList.indexOf(observer);
        if (index >= 0) observerList.remove(index);
    }
 
    @Override
    public void notifyObservers() {
        for (Observer observer : observerList) observer.update(newspaper);
    }
}

Concrete Implementation – Observer:
Observers are interested in receiving notifications from the Subject.

public class Person implements NewspaperObserver {
    @Override
    public void update(Newspaper newspaper) {
        newspaper.getDetails();
    }
}

Summary:
The observer pattern is maintainable, extendable, and loosely coupled. Any object can become a Observer or a Subject with ease. Note, the order in which update() is invoked may alter across runtimes. This example can be extended to limit when notifications are sent (for example if the publishListener() could be invoked too many times).

Open the comment form

Leave a comment:

Comments will be reviewed before they are posted.

User Comments:

My Design Patterns Catalog - Riley MacDonald on 2017-12-04 23:50:41 said:
[…] Observer: The Observer Pattern utilizes a one to many relationship to notify interested objects of changes. Observers register with a Subject to be notified of updates. […]