Jump to content

Observer pattern

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 67.77.88.7 (talk) at 14:13, 30 May 2012 (Added the prggmr event library as an implementation). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The observer pattern (aka. Dependents, publish/subscribe) 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. It is mainly used to implement distributed event handling systems. Observer is also a key part in the familiar MVC architectural pattern. In fact the observer pattern was first implemented in Smalltalk's MVC based user interface framework.[1]

Related patterns: mediator, singleton.

Structure

UML diagram of Observer pattern

Definition

The essence of the Observer Pattern is to "Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. ".[1]

Example

Below is an example 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.

/* File Name : EventSource.java */
package org.wikipedia.obs;

import java.util.Observable;          //Observable is here
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class EventSource extends Observable implements Runnable {
    public void run() {
        try {
            final InputStreamReader isr = new InputStreamReader(System.in);
            final BufferedReader br = new BufferedReader(isr);
            while (true) {
                String response = br.readLine();
                setChanged();
                notifyObservers(response);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
/* File Name: ResponseHandler.java */

package org.wikipedia.obs;

import java.util.Observable;
import java.util.Observer;  /* this is Event Handler */

public class ResponseHandler implements Observer {
    private String resp;
    public void update(Observable obj, Object arg) {
        if (arg instanceof String) {
            resp = (String) arg;
            System.out.println("\nReceived Response: " + resp );
        }
    }
}
/* Filename : MyApp.java */
/* This is the main program */

package org.wikipedia.obs;

public class MyApp {
    public static void main(String[] args) {
        System.out.println("Enter Text >");

        // create an event source - reads from stdin
        final EventSource eventSource = new EventSource();

        // create an observer
        final ResponseHandler responseHandler = new ResponseHandler();

        // subscribe the observer to the event source
        eventSource.addObserver(responseHandler);

        // starts the event thread
        Thread thread = new Thread(eventSource);
        thread.start();
    }
}


Implementations

The observer pattern is implemented in numerous programming libraries and systems, including almost all GUI toolkits.

Some of the most notable implementations of this pattern:

ActionScript

C

  • GObject, in GLib - an implementation of objects and signals/callbacks in C. (This library has many bindings to other programming languages.)

C++

Objective-C

C#

ColdFusion

Delphi

Java

  • The class java.util.Observer[2] provides a simple observer implementation.
  • Events are typically implemented in Java through the callback pattern: one piece of code provides a common interface with as many methods as many events are required,[3] another piece of code provides an implementation of that interface, and another one receives that implementation, and raises events as necessary.[4]

JavaScript

  • DevShop DevShop Js is a pocket-sized minimalist framework of common design patterns for JavaScript. Includes Observer pattern.
  • The observer pattern is implemented in jQuery using '.on()', '.trigger()' and 'off()' to create, fire and remove event handlers. jQuery uses the actual in-memory DOM Elements by attaching event handlers to them, and automatically removing the events when an element disappears from the DOM. See this jQuery Event Model slideshow for more info.

Lisp

  • Cells, a dataflow extension to Common Lisp that uses meta-programming to hide some of the details of Observer pattern implementation.

Perl

PHP

Python

Ruby

  • Observer, from the Ruby Standard Library.

Other/Misc

  • CSP - Observer Pattern using CSP-like Rendezvous (each actor is a process, communication is via rendezvous).
  • YUI Event utility implements custom events through the observer pattern
  • Publish/Subscribe with LabVIEW, Implementation example of Observer or Publish/Subscribe using G.

Critics

The Observer pattern is criticized[5] for being too verbose, introducing too many bugs and violating software engineering principles, such as not promoting side-effects, encapsulation, composability, separation of concepts, scalability, uniformity, abstraction, resource management, semantic distance [citation needed]. The recommended approach is to gradually deprecate observers in favor of reactive programming abstractions.

See also

References