2017-02-10 | ../../ | Slides

Observer Pattern

1 Observer Pattern

  1. "The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods." – Wikipedia
  2. Also called Publish-Subscribe (Pub-Sub)
  3. Reactive Programming is an extension of the Observer Software Design Pattern.

1.2 A Simple Example

  1. https://en.wikipedia.org/wiki/Observer_pattern Example Java code. Skip Python code.

2 Another Simple Example: A Zoo System

"Create a zoo system that allows a client to register and unregister listeners that will be notified when an animal is added to the zoo. In addition, create a concerete listener that will print the name of the animal that is added to the zoo."

2.1 Animal POJO (Plain Old Java Object)

  1. An Animal class that represents an animal with a name.
    class Animal {
        private String name;
        public Animal (String nm)       { name = nm;}
        public String getName ()        { return name;}
        public void setName (String nm) { name = nm;}

2.2 AnimalAddedListener

  1. The Observer interface.
    interface AnimalAddedListener {
        public void onAnimalAdded (Animal animal);

2.3 PrintNameAnimalAddedListener

  1. A concrete observer class. Prints the name of the animal added to the zoo.
    class PrintNameAnimalAddedListener implements AnimalAddedListener {
        public void onAnimalAdded (Animal a) {
            System.out.println("New animal '" + a.getName() + "' added");

2.4 class Zoo

  1. A Zoo class that will act as the subject. Stores a list of the animals in the zoo. Notifies registered listeners each time a new animal is added to the zoo
    public class Zoo {
        private List <Animal> animals = new ArrayList <> ();
        private List <AnimalAddedListener> listeners = new ArrayList <> ();
        public void addAnimal (Animal a) {
        public void registerAnimalAddedListener (AnimalAddedListener listener) {
        public void unregisterAnimalAddedListener (AnimalAddedListener listener) {
        protected void notifyAnimalAddedListeners (Animal a) {
            listeners.forEach(listener -> listener.onAnimalAdded(a)); // java8

2.5 Main.java

public class Main {
    public static void main (String[] args) {
        Zoo zoo = new Zoo();
        zoo.registerAnimalAddedListener(new PrintNameAnimalAddedListener());
        zoo.addAnimal(new Animal("Tiger"));

3 Example #2: Reactive Examples in Android

  1. Reactive Programming is an extension of the Observer Software Design Pattern.
  2. Ex#1: Hello World, Once Again
  3. Ex#2: Asynchronous Example
  4. Ex#3: Publish a List
  5. All the above in ../AppDevelop/reactiveAndroid.html

4 References

  1. Oracle.com, Java Essentials Tutorial, https://docs.oracle.com/javase/tutorial/essential/TOC.html. This is the recommended tutorial. Reference.
  2. https://dzone.com/articles/the-observer-pattern-using-modern-java The Observer Pattern Using Java 8, 2016. The Zoo example of above is from here. More J8 concepts are discussed. Recommended Reading.

5 End

Copyright © 2017 www.wright.edu/~pmateti 2017-02-10