Observer Pattern

By | March 15, 2017

Observer Pattern defines a one-to-many relationship between a set of objects. When the state of one object changes, all of its dependents are notified/updated automatically. It also belongs to Behavior pattern group.

1. A customized notification system.

Several newspaper agencies like CNN, NYTime, BBC are subscribed to a feed of new tweets and may want to receive a notification if a tweet contains particular keyword. we call newspaper agencies the Observers and the customized notification (like Tweeter) the Subject.

2. Solution.

2.1 The Observer Pattern defined: the class diagram.

Designing the notification in details:

2.2 Interdependency between Subject and Observer.

Observer Pattern makes it possible for a object design where subjects and observers are loosely coupled, they can interact but have very little knowledge of each other. This is because of some reasons below (cited Design Patterns – HeadFirst for references):

  • The only thing the subject knows about an observer is that it implements a certain interface (the Observer interface). It doesn’t need to know the concrete class of the observer, what it does, or anything else about it.
  • We can remove or add new observers at any time. Because the only thing the subject depends on is a list of objects that implement the Observer interface, we can add new observers whenever we want. In fact, we can replace any observer at runtime with another observer and the subject will keep purring along.
  • We never need to modify the subject to add new types of observers. If we have a new concrete observer, the only thing that we have to do is implement the Observer interface in the new class and register as an observer.
  • We can reuse subjects or observers independently of each other. If we have another use for a subject or an observer, we can easily reuse them because the two aren’t tightly coupled.
  • Changes to either the subject or an observer will not affect the other. Because the two are loosely coupled, we are free to make changes to either, as long as the objects still meet their obligations to implement the subject or observer interfaces.

3. Implementation.

Define an Observer interface just has one method called notify that will be called by subject(Feed) when a new tweet is available.

public interface Observer {
    void nofifyObserver(String tweet);

Define different observers (newspapers) that produce a different action for each different keyword contains in a tweet:

public class CNN implements Observer {
    public void nofifyObserver(String tweet) {
        System.out.println("Notify to CNN Observer");
        if(tweet != null && tweet.contains("Trump")) {
            System.out.println("CNN Breaking New:" + tweet);

public class NYTime implements Observer {
    public void nofifyObserver(String tweet) {
        System.out.println("Notify to NYTime Observer");
        if(tweet != null && tweet.contains("money")) {
            System.out.println("NYTime Breaking New: " + tweet);

Subject interface defines two methods: registerObserver and notifyObserver. The subject can register a new observer and notify his observers of a tweet:

public interface Subject {
    void registerObserver(Observer o);
    void notifyObservers(String tweet);

Feed class implementation keeps an internal list of observers that it can then notify to all the observer list when a tweet arrives.

import java.util.ArrayList;
import java.util.List;

public class Feed implements Subject {
    private List<Observer> observers = new ArrayList<>();

    public void registerObserver(Observer o) {

    public void notifyObservers(String tweet) {
        observers.forEach(o -> o.nofifyObserver(tweet));

Implement ObserverTest and run to see how it look like.

public class ObserverTest {
    public static void main(String [] args) {
        Feed f = new Feed();
        f.registerObserver(new CNN());
        f.registerObserver(new NYTime());
        f.notifyObservers("Trump president has just issued a new migration rule.");

When a new tweet comes, the system notifies to both CNN and NYTime. Unsurprisingly, the CNN picks up this tweet.

Notify to CNN Observer
CNN Breaking New:Trump president has just issued a new migration rule.
Notify to NYTime Observer


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.