Observer

Memento Computer Science Design Patterns
Observer
State

Scope

Object

Purpose

Behavioral

Intent

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Applicability

  • when an abstraction has two aspects, one dependent on the other
  • when a change to one object requires changing others, and you don't know how many objects need to be changed
  • when an object should notify other objects without making assumptions about who these objects are

Structure

Observer.svg

Consequences

  • + modularity: subject and observers may vary independently
  • + extensibility: can define and add any number of observers
  • + customizability: different observers provide different views of subject
  • - unexpected updates: observers don't know about each other
  • - update overhead: might need hints

Implementation

  • subject-observer mapping
  • dangling references
  • avoiding observer-specific update protocols: the push and pull models
  • registering modifications of interest explicitly

Related pattern

  • Singleton, is used to make observable object unique and accessible globally.
  • Mediator, is used to encapsulate updated objects

Description

Problem 
In one place or many places in the application we need to be aware about a system event or an application state change. We'd like to have a standard way of subscribing to listening for system events and a standard way of notifying the interested parties. The notification should be automated after an interested party subscribed to the system event or application state change. There should be a way to unsubscribe, too.
Forces 
Observers and observables probably should be represented by objects. The observer objects will be notified by the observable objects.
Solution 
After subscribing the listening objects will be notified by a way of method call.
Loose coupling
When two objects are loosely coupled, they can interact, but they have very little knowledge of each other. Strive for loosely coupled designs between objects that interact.
  • The only thing that Subject knows about an observer is that it implements a certain interface
  • We can add new observers at any time
  • We never need to modify the subject to add new types of observers
  • We can reuse subjects or observers independently of each other
  • Changes to either the subject or an observer will not affect the other

Examples

The Observer pattern is used extensively in Java. E.g. in the following piece of code

  • button is the Subject
  • MyListener is the Observer
  • actionPerformed() is the equivalent to update()
  1.  JButton button = new JButton("Click me!");
    
  2.  button.addActionListener(new MyListener());
    
  3.  
    
  4.  class MyListener implements ActionListener {
    
  5.     public void actionPerformed(ActionEvent event) {
    
  6.        ...
    
  7.     }  
    
  8.  }
    

Another example is the PropertyChangeSupport. The Component class uses a PropertyChangeSupport object to let interested observers register for notification of changes in the properties of labels, panels, and other GUI components.

Observer-PropertyChangeSupport.gif

Can you find the Subject, Observer and update() in the above class diagram?

  • Component is the Subject
  • PropertyChangeListener is the Observer
  • propertyChange() is the equivalent to update()

Cost

This pattern can be tricky if you do not beware. Beware the data representing the state changes of the subject can evolve without changing the interfaces. If you only transmit a string, the pattern could become very expensive if at least one new observer needs also a state code. You should use a mediator unless you know the implementation of the subject state will never change.

Creation

This pattern has a cost to create.

Maintenance

This pattern can be expensive to maintain.

Removal

This pattern has a cost to remove too.

Advises

  • Put the subject and observer term in the name of the subject and the observer classes to indicate the use of the pattern to the other developers.
  • To improve performances, you can only send to the observers the difference of states instead of the new state. The observers can only update following the changed part of the subject instead of all the state of the subject.

Implementations

Implementation in ActionScript 3
// Main Class
package {
    import flash.display.MovieClip;
 
    public class Main extends MovieClip {
        private var _cs:ConcreteSubject = new ConcreteSubject();
        private var _co1:ConcreteObserver1 = new ConcreteObserver1();
        private var _co2:ConcreteObserver2 = new ConcreteObserver2();
 
        public function Main() {
            _cs.registerObserver(_co1);
            _cs.registerObserver(_co2);
 
            _cs.changeState(10);
            _cs.changeState(99);
 
            _cs.unRegisterObserver(_co1);
 
            _cs.changeState(17);
 
            _co1 = null;
        }
    }
}
 
// Interface Subject
package {
    public interface ISubject {
        function registerObserver(o:IObserver):void;
 
        function unRegisterObserver(o:IObserver):void;
 
        function updateObservers():void;
 
        function changeState(newState:uint):void;
    }
}
 
// Interface Observer
package {
    public interface IObserver {
        function update(newState:uint):void;
    }
}
 
// Concrete Subject
package {
    public class ConcreteSubject implements ISubject {
        private var _observersList:Array = new Array();
        private var _currentState:uint;
 
        public function ConcreteSubject() {
        }
 
        public function registerObserver(o:IObserver):void {
            _observersList.push( o );
            _observersList[_observersList.length-1].update(_currentState); // update newly registered
        }
 
        public function unRegisterObserver(o:IObserver):void {
            _observersList.splice( _observersList.indexOf( o ), 1 );
        }
 
        public function updateObservers():void {
            for( var i:uint = 0; i<_observersList.length; i++) {
                _observersList[i].update(_currentState);
            }
        }
 
        public function changeState(newState:uint):void {
            _currentState = newState;
            updateObservers();
        }
    }
}
 
// Concrete Observer 1
package {
    public class ConcreteObserver1 implements IObserver {
        public function ConcreteObserver1() {
        }
 
        public function update(newState:uint):void {
            trace( "co1: "+newState );
        }
 
        // other Observer specific methods
    }
}
 
// Concrete Observer 2
package {
    public class ConcreteObserver2 implements IObserver {
        public function ConcreteObserver2() {
        }
 
        public function update(newState:uint):void {
            trace( "co2: "+newState );
        }
 
        // other Observer specific methods
    }
}
Implementation in C#

Traditional Method

C# and the other .NET Framework languages do not typically require a full implementation of the Observer pattern using interfaces and concrete objects. Here is an example of using them, however.

 
 
using System;
using System.Collections;
 
namespace Wikipedia.Patterns.Observer
{
  // IObserver --> interface for the observer
  public interface IObserver
  {
    // called by the subject to update the observer of any change
    // The method parameters can be modified to fit certain criteria
    void Update(string message);
  }
 
  public class Subject
  {
    // use array list implementation for collection of observers
    private ArrayList observers;
 
    // constructor
    public Subject()
    {
      observers = new ArrayList();
    }
 
    public void Register(IObserver observer)
    {
      // if list does not contain observer, add
      if (!observers.Contains(observer))
      {
        observers.Add(observer);
      }
    }
 
    public void Unregister(IObserver observer)
    {
      // if observer is in the list, remove
      observers.Remove(observer);
    }
 
    public void Notify(string message)
    {
      // call update method for every observer
      foreach (IObserver observer in observers)
      {
        observer.Update(message);
      }
    }
  }
 
  // Observer1 --> Implements the IObserver
  public class Observer1 : IObserver
  {
    public void Update(string message)
    {
      Console.WriteLine("Observer1:" + message);
    }
  }
 
  // Observer2 --> Implements the IObserver
  public class Observer2 : IObserver
  {
    public void Update(string message)
    {
      Console.WriteLine("Observer2:" + message);
    }
  }
 
  // Test class
  public class ObserverTester
  {
    [STAThread]
    public static void Main()
    {
      Subject mySubject = new Subject();
      IObserver myObserver1 = new Observer1();
      IObserver myObserver2 = new Observer2();
 
      // register observers
      mySubject.Register(myObserver1);
      mySubject.Register(myObserver2);
 
      mySubject.Notify("message 1");
      mySubject.Notify("message 2");
    }
  }
}

Using Events

The alternative to using concrete and abstract observers and publishers in C# and other .NET Framework languages, such as Visual Basic, is to use events. The event model is supported via delegates that define the method signature that should be used to capture events. Consequently, delegates provide the mediation otherwise provided by the abstract observer, the methods themselves provide the concrete observer, the concrete subject is the class defining the event, and the subject is the event system built into the base class library. It is the preferred method of accomplishing the Observer pattern in .NET applications.

using System;
 
// First, declare a delegate type that will be used to fire events.
//  This is the same delegate as System.EventHandler.
//  This delegate serves as the abstract observer.
//  It does not provide the implementation, but merely the contract.
public delegate void EventHandler(object sender, EventArgs e);
 
// Next, declare a published event.  This serves as the concrete subject.
//  Note that the abstract subject is handled implicitly by the runtime.
public class Button
{
    // The EventHandler contract is part of the event declaration.
    public event EventHandler Clicked;
 
    // By convention, .NET events are fired from descendant classes by a virtual method,
    //  allowing descendant classes to handle the event invocation without subscribing
    //  to the event itself.
    protected virtual void OnClicked(EventArgs e)
    {
        if (Clicked != null)
            Clicked(this, e); // implicitly calls all observers/subscribers
    }
}
 
// Then in an observing class, you are able to attach and detach from the events:
public class Window
{
    private Button okButton;
 
    public Window()
    {
        okButton = new Button();
        // This is an attach function.  Detaching is accomplished with -=.
        // Note that it is invalid to use the assignment operator - events are multicast
        //  and can have multiple observers.
        okButton.Clicked += new EventHandler(okButton_Clicked);
    }
 
    private void okButton_Clicked(object sender, EventArgs e)
    {
        // This method is called when Clicked(this, e) is called within the Button class
        //  unless it has been detached.
    }
}
Implementation in Java

Handy implementation

You can implement this pattern in Java like this:

  1.  // Observer pattern -- Structural example
    
  2.  // @since JDK 5.0
    
  3.  import java.util.ArrayList;
    
  4.  
    
  5.  // "Subject"
    
  6.  abstract class Subject {
    
  7.     // Fields
    
  8.     private ArrayList<Observer> observers = new ArrayList<Observer>();
    
  9.     // Methods
    
  10.     public void attach(Observer observer) {
    
  11.        observers.add(observer);
    
  12.     }
    
  13.     public void detach(Observer observer) {
    
  14.        observers.remove(observer);
    
  15.     }
    
  16.     public void notifyObservers() {
    
  17.        for (Observer o : observers)    
    
  18.           o.update();
    
  19.    }
    
  20.  }
    
  1.  // "ConcreteSubject"
    
  2.  class ConcreteSubject extends Subject {
    
  3.   // Fields
    
  4.   private String subjectState;
    
  5.   // Properties
    
  6.   public String getSubjectState() {
    
  7.     return subjectState;
    
  8.   }
    
  9.   public void setSubjectState(String value) {
    
  10.     subjectState = value;
    
  11.   }
    
  12.  }
    
  1.  // "Observer"
    
  2.  abstract class Observer {
    
  3.    // Methods
    
  4.    abstract public void update();
    
  5.  }
    
  1.  // "ConcreteObserver"
    
  2.  class ConcreteObserver extends Observer {
    
  3.   // Fields
    
  4.   private String name;
    
  5.   private String observerState;
    
  6.   private ConcreteSubject subject;
    
  7.  
    
  8.   // Constructors
    
  9.   public ConcreteObserver(ConcreteSubject subject, String name) {
    
  10.      this.subject = subject;
    
  11.      this.name = name;
    
  12.      //subject.attach(this);
    
  13.   }
    
  14.   // Methods
    
  15.   public void update() {
    
  16.      observerState = subject.getSubjectState();
    
  17.      System.out.printf("Observer %s's new state is %s\n", name, observerState);
    
  18.   }
    
  19.  }
    
  1.  // Client test
    
  2.  public class Client {
    
  3.    public static void main(String[] args) {
    
  4.       // Configure Observer structure
    
  5.       ConcreteSubject s = new ConcreteSubject();
    
  6.       s.attach(new ConcreteObserver(s, "A"));
    
  7.       s.attach(new ConcreteObserver(s, "B"));
    
  8.       s.attach(new ConcreteObserver(s, "C"));
    
  9.  
    
  10.       // Change subject and notify observers
    
  11.       s.setSubjectState("NEW");
    
  12.       s.notifyObservers();
    
  13.    }
    
  14.  }
    

Built-in support

The Java JDK has several implementations of this pattern: application in Graphical User Interfaces such as in the AWK toolkit, Swing etc. In Swing, whenever a user clicks a button or adjusts a slider, many objects in the application may need to react to the change. Swing refers to interested clients (observers) as "listeners" and lets you register as many listeners as you like to be notified of a component's events.

MVC is more M(VC) in Swing, i.e. View and Controller are tightly coupled; Swing does not divide Views from Controllers. MVC supports n-tier development, i.e. loosely coupled layers (see below) that can change independently and that may even execute on different machines.

There is also a built-in support for the Observer pattern. All one has to do is extend java.util.Observable (the Subject) and tell it when to notify the java.util.Observers. The API does the rest for you. You may use either push or pull style of updating your observers.

Observer-java.jpg

java.util.Observable is a class while java.util.Observer is an interface.

  1.  public void setValue(double value) {
    
  2.     this.value = value;
    
  3.     setChanged();
    
  4.     notifyObservers();
    
  5.  }
    

Note that you have to call setChanged() so that the Observable code will broadcast the change. The notifyObservers() method calls the update() method of each registered observer. The update() method is a requirement for implementers of the Observer Interface.

  1.  // Observer pattern -- Structural example
    
  2.  import java.util.Observable;
    
  3.  import java.util.Observer;
    
  4.  
    
  5.  // "Subject"
    
  6.  class ConcreteSubject extends Observable {
    
  7.   // Fields
    
  8.   private String subjectState;
    
  9.   // Methods
    
  10.   public void dataChanged() {
    
  11.      setChanged();
    
  12.      notifyObservers(); // use the pull method
    
  13.   }
    
  14.   // Properties
    
  15.   public String getSubjectState() {
    
  16.      return subjectState;
    
  17.   }
    
  18.   public void setSubjectState(String value) {
    
  19.       subjectState = value;
    
  20.       dataChanged();
    
  21.   }
    
  22.  }
    
  1.  // "ConcreteObserver"
    
  2.  import java.util.Observable;
    
  3.  import java.util.Observer;
    
  4.  
    
  5.  class ConcreteObserver implements Observer {
    
  6.    // Fields
    
  7.    private String name;
    
  8.    private String observerState;
    
  9.    private Observable subject;
    
  10.  
    
  11.    // Constructors
    
  12.    public ConcreteObserver(Observable subject, String name) {
    
  13.      this.subject = subject;
    
  14.      this.name = name;
    
  15.      subject.addObserver(this);
    
  16.   }
    
  17.  
    
  18.   // Methods
    
  19.   public void update(Observable subject, Object arg) {
    
  20.    if (subject instanceof ConcreteSubject) {
    
  21.      ConcreteSubject subj = (ConcreteSubject)subject;
    
  22.      observerState = subj.getSubjectState();
    
  23.      System.out.printf("Observer %s's new state is %s\n", name, observerState);
    
  24.    }
    
  25.   }
    
  26.  }
    
  1.  // Client test
    
  2.  public class Client {
    
  3.    public static void main(String[] args) {
    
  4.       // Configure Observer structure
    
  5.       ConcreteSubject s = new ConcreteSubject();
    
  6.       new ConcreteObserver(s, "A");
    
  7.       new ConcreteObserver(s, "B");
    
  8.       new ConcreteObserver(s, "C");
    
  9.       // Change subject and notify observers
    
  10.       s.setSubjectState("NEW");
    
  11.    }
    
  12.  }
    

Keyboard handling

Below is an example written in Java that takes keyboard input and treats each input line as an event. The example is built upon the library classes java.util.Observer and java.util.Observable. When a string is supplied from System.in, the method notifyObservers is then called, in order to notify all observers of the event's occurrence, in the form of an invocation of their 'update' methods - in our example, ResponseHandler.update(...).

The file MyApp.java contains a main() method that might be used in order to run the code.

  1. /* Filename : EventSource.java */
    
  2. package org.wikibooks.obs;
    
  3.  
    
  4. import java.util.Observable;          // Observable is here
    
  5. import java.io.BufferedReader;
    
  6. import java.io.IOException;
    
  7. import java.io.InputStreamReader;
    
  8.  
    
  9. public class EventSource extends Observable implements Runnable {
    
  10.     @Override
    
  11.     public void run() {
    
  12.         try {
    
  13.             final InputStreamReader isr = new InputStreamReader(System.in);
    
  14.             final BufferedReader br = new BufferedReader(isr);
    
  15.             while (true) {
    
  16.                 String response = br.readLine();
    
  17.                 setChanged();
    
  18.                 notifyObservers(response);
    
  19.             }
    
  20.         } catch (IOException e) {
    
  21.             e.printStackTrace();
    
  22.         }
    
  23.     }
    
  24. }
    
  1. /* Filename : ResponseHandler.java */
    
  2.  
    
  3. package org.wikibooks.obs;
    
  4.  
    
  5. import java.util.Observable;
    
  6. import java.util.Observer;  /* this is Event Handler */
    
  7.  
    
  8. public class ResponseHandler implements Observer {
    
  9.     private String resp;
    
  10.     public void update(Observable obj, Object arg) {
    
  11.         if (arg instanceof String) {
    
  12.             resp = (String) arg;
    
  13.             System.out.println("\nReceived Response: " + resp );
    
  14.         }
    
  15.     }
    
  16. }
    
  1. /* Filename : MyApp.java */
    
  2. /* This is the main program */
    
  3.  
    
  4. package org.wikibooks.obs;
    
  5.  
    
  6. public class MyApp {
    
  7.     public static void main(String[] args) {
    
  8.         System.out.println("Enter Text >");
    
  9.  
    
  10.         // create an event source - reads from stdin
    
  11.         final EventSource eventSource = new EventSource();
    
  12.  
    
  13.         // create an observer
    
  14.         final ResponseHandler responseHandler = new ResponseHandler();
    
  15.  
    
  16.         // subscribe the observer to the event source
    
  17.         eventSource.addObserver(responseHandler);
    
  18.  
    
  19.         // starts the event thread
    
  20.         Thread thread = new Thread(eventSource);
    
  21.         thread.start();
    
  22.     }
    
  23. }
    

The Java implementation of the Observer pattern has pros and cons:

Pros

  • It hides many of the details of the Observer pattern
  • It can be used both pull and push ways.

Cons

  • Because Observable is a class, you have to subclass it; you can’t add on the Observable behavior to an existing class that subclasses another superclass (fails the programming to interfaces principle). If you can’t subclass Observable, then use delegation, i.e. provide your class with an Observable object and have your class forward key method calls to it.
  • Because setChanged() is protected, you can’t favour composition over inheritance.
Implementation in PHP

class STUDENT

<?php
class Student implements SplObserver {
 
  protected $type = "Student";
  private   $name;
  private   $address;
  private   $telephone;
  private   $email;
  private   $_classes = array();
 
  public function __construct($name)
  {
    $this->name = $name;
  }
 
  public function GET_type()
  {
    return $this->type;
  }
 
  public function GET_name()
  {
    return $this->name;
  }
 
  public function GET_email()
  {
    return $this->email;
  }
 
  public function GET_telephone()
  {
    return $this->telephone;
  }
 
  public function update(SplSubject $object)
  {
    $object->SET_log("Comes from ".$this->name.": I'm a student of ".$object->GET_materia());
  }
 
}
 
?>

class TEACHER

<?php
class Teacher implements SplObserver {
 
  protected $type = "Teacher";
  private   $name;
  private   $address;
  private   $telephone;
  private   $email;
  private   $_classes = array();
 
  public function __construct($name)
  {
    $this->name = $name;
  }
 
  public function GET_type()
  {
    return $this->type;
  }
 
  public function GET_name()
  {
    return $this->name;
  }
 
  public function GET_email()
  {
    return $this->email;
  }
 
  public function GET_telephone()
  {
    return $this->name;
  }
 
  public function update(SplSubject $object)
  {
    $object->SET_log("Comes from ".$this->name.": I teach in ".$object->GET_materia());
  }
 
}
 
?>

Class SUBJECT

<?php
 
class Subject implements SplSubject {
 
  private $name_materia;
  private $_observers = array();
  private $_log = array();
 
  function __construct($name)
  {
    $this->name_materia = $name;
    $this->_log[]       = "Subject $name was included";
  }
 
  /* Add an observer */
  public function attach(SplObserver $classes) {
    $this->_classes[] = $classes;
    $this->_log[]     = " The ".$classes->GET_type()." ".$classes->GET_name()." was included";
  }
 
  /* Remove an observer */
  public function detach(SplObserver $classes) {
    foreach ($this->_classes as $key => $obj) {
      if ($obj == $classes) {
        unset($this->_classes[$key]);
        $this->_log[] = " The ".$classes->GET_type()." ".$classes->GET_name()." was removed";
                }
    }
  }
 
  /* Notificate an observer */
  public function notify(){
    foreach ($this->_classes as $classes){
      $classes->update($this);
    }
  }
 
  public function GET_materia()
  {
    return $this->name_materia;
  }
 
  function SET_log($valor)
  {
    $this->_log[] = $valor ;
  }
 
  function GET_log()
  {
    return $this->_log;
  }
 
 
}
?>

Application

<?php
require_once("teacher.class.php");
require_once("student.class.php");
require_once("subject.class.php");
 
$subject  = new Subject("Math");
$marcus   = new Teacher("Marcus Brasizza");
$rafael   = new Student("Rafael");
$vinicius = new Student("Vinicius");
 
// Include observers in the math Subject
$subject->attach($rafael);
$subject->attach($vinicius);
$subject->attach($marcus);
 
$subject2 = new Subject("English");
$renato   = new Teacher("Renato");
$fabio    = new Student("Fabio");
$tiago    = new Student("Tiago");
 
// Include observers in the english Subject
$subject2->attach($renato);
$subject2->attach($vinicius);
$subject2->attach($fabio);
$subject2->attach($tiago);
 
// Remove the instance "Rafael from subject"
$subject->detach($rafael);
 
// Notify both subjects
$subject->notify();
$subject2->notify();
 
echo "First Subject <br>";
echo "<pre>";
print_r($subject->GET_log());
echo "</pre>";
echo "<hr>";
echo "Second Subject <br>";
echo "<pre>";
print_r($subject2->GET_log());
echo "</pre>";
?>

OUTPUT

First Subject

Array
(
    [0] =>  Subject Math was included
    [1] =>  The Student Rafael was included
    [2] =>  The Student Vinicius was included
    [3] =>  The Teacher Marcus Brasizza was included
    [4] =>  The Student Rafael was removed
    [5] => Comes from Vinicius: I'm a student of Math
    [6] => Comes from Marcus Brasizza: I teach in Math
)

Second Subject

Array
(
    [0] =>  Subject English was included
    [1] =>  The Teacher Renato was included
    [2] =>  The Student Vinicius was included
    [3] =>  The Student Fabio was included
    [4] =>  The Student Tiago was included
    [5] => Comes from Renato: I teach in English
    [6] => Comes from Vinicius: I'm a student of English
    [7] => Comes from Fabio: I'm a student of English
    [8] => Comes from Tiago: I'm a student of English
)
Implementation in Python

The observer pattern in Python:

class AbstractSubject:
    def register(self, listener):
        raise NotImplementedError("Must subclass me")
 
    def unregister(self, listener):
        raise NotImplementedError("Must subclass me")
 
    def notify_listeners(self, event):
        raise NotImplementedError("Must subclass me")
 
class Listener:
    def __init__(self, name, subject):
        self.name = name
        subject.register(self)
 
    def notify(self, event):
        print self.name, "received event", event
 
class Subject(AbstractSubject):
    def __init__(self):
        self.listeners = []
        self.data = None
 
    def getUserAction(self):
        self.data = raw_input('Enter something to do:')
        return self.data
 
    # Implement abstract Class AbstractSubject
 
    def register(self, listener):
        self.listeners.append(listener)
 
    def unregister(self, listener):
        self.listeners.remove(listener)
 
    def notify_listeners(self, event):
        for listener in self.listeners:
            listener.notify(event)
 
 
if __name__=="__main__":
    # make a subject object to spy on
    subject = Subject()
 
    # register two listeners to monitor it.
    listenerA = Listener("<listener A>", subject)
    listenerB = Listener("<listener B>", subject)
 
    # simulated event
    subject.notify_listeners ("<event 1>")
    # outputs:
    #     <listener A> received event <event 1>
    #     <listener B> received event <event 1>
 
    action = subject.getUserAction()
    subject.notify_listeners(action)
    #Enter something to do:hello
    # outputs:
    #     <listener A> received event hello
    #     <listener B> received event hello

The observer pattern can be implemented more succinctly in Python using function decorators.

Implementation in Ruby

In Ruby, use the standard Observable mixin. For documentation and an example, see http://www.ruby-doc.org/stdlib/libdoc/observer/rdoc/index.html


Clipboard

To do:
Add more illustrations.


Memento Computer Science Design Patterns
Observer
State


You have questions about this page?
Ask it here:


Create a new page on this book:

Last modified on 4 March 2014, at 19:43