Design patterns Observer / Java


Example

The observer pattern lets users of a class subscribe to events that happen when this class processes data etc. and be notified when these event occur. In the following example we create a processing class and an observer class which will be notified while processing a phrase - if it finds words that are longer than 5 letters.

The LongWordsObserver interface defines the observer. Implement this interface in order to register an observer to events.

// an observe that can be registered and receive notifications
public interface LongWordsObserver {
    void notify(WordEvent event);
}

The WordEvent class is the event that will be sent to the observer classes once certain events occur (in this case, long words were found)

// An event class which contains the long word that was found
public class WordEvent {

    private String word;

    public WordEvent(String word) {
        this.word = word;
    }

    public String getWord() {
        return word;
    }
}

The PhraseProcessor class is the class that processes the given phrase. It allows observers to be registered using the addObserver method. Once long words are found, these observers will be called using an instance of the WordEvent class.

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

public class PhraseProcessor {

    // the list of observers
    private List<LongWordsObserver> observers = new ArrayList<>();

    // register an observer
    public void addObserver(LongWordsObserver observer) {
        observers.add(observer);
    }

    // inform all the observers that a long word was found
    private void informObservers(String word) {
        observers.forEach(o -> o.notify(new WordEvent(word)));
    }

    // the main method - process a phrase and look for long words. If such are found,
    // notify all the observers
    public void process(String phrase) {
        for (String word : phrase.split(" ")) {
            if (word.length() > 5) {
                informObservers(word);
            }
        }
    }
}

The LongWordsExample class shows how to register observers, call the process method and receive alerts when long words were found.

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

public class LongWordsExample {

    public static void main(String[] args) {

        // create a list of words to be filled when long words were found
        List<String> longWords = new ArrayList<>();

        // create the PhraseProcessor class
        PhraseProcessor processor = new PhraseProcessor();

        // register an observer and specify what it should do when it receives events,
        // namely to append long words in the longwords list
        processor.addObserver(event -> longWords.add(event.getWord()));

        // call the process method 
        processor.process("Lorem ipsum dolor sit amet, consectetuer adipiscing elit");

        // show the list of long words after the processing is done
        System.out.println(String.join(", ", longWords));
        // consectetuer, adipiscing
    }
}