Observer pattern
This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (July 2009) |
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
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
- flash.events, a package in ActionScript 3.0 (following from the mx.events package in ActionScript 2.0).
C
- GObject, in GLib - an implementation of objects and signals/callbacks in C. (This library has many bindings to other programming languages.)
C++
- libsigc++ - the C++ signalling template library.
- sigslot - C++ Signal/Slot Library
- Cpp::Events - Template-based C++ implementation that introduces separation of connection management interface of the event object from the invocation interface.
- XLObject - Template-based C++ signal/slot model patterned after Qt.
- Signals - A lightweight and non-intrusive C++ signal/slot model implementation.
- libevent - Multi-threaded Crossplatform Signal/Slot C++ Library
- Boost.Signals, an implementation of signal/slot model
- MFC's CDocument-CView-framework
- The Qt C++ framework's signal/slot model
- The Advanced Component Framework's component-based Model/Observer pattern implementation.
- The MRPT robotics C++ framework's observer/observable model.
Objective-C
- NSNotificationCenter - The Objective-C NSNotificationCenter class.
- NSKeyValueObserving - The Objective-C NSKeyValueObserving protocol.
C#
- The IObserver<T> Interface - The .NET Framework 4+ supported way of implementing the observer pattern.
- Exploring the Observer Design Pattern - the C# and Visual Basic .NET implementation, using delegates and the Event pattern
ColdFusion
- http://www.cfdesignpatterns.com/behavioral-patterns/observer-design-pattern-in-coldfusion/
- http://coldfusioneventmanager.riaforge.org/
Delphi
- Delphi Observer Pattern, a Delphi implementation
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
- EventDispatcher singleton, a JavaScript core API based Signals and slots implementation - an observer concept different from Publish/subscribe - pretty lightweighted but still type-safety enforcing.
- 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
- Class::Observable Basic observer pattern implementation
- Class::Publisher a slightly more advanced implementation
- POE::Component::Syndicator Observer pattern for POE
PHP
- Symfony2 Event Dispatcher, a standalone library component by the Symfony team
- Event_Dispatcher, a PEAR implementation
- SPLSubject & SPLObserver, part of the Standard PHP Library
- prggmr, an event processing engine for PHP 5.4+ designed to be lightweight, fast and very simple to use.
Python
- Louie, an implementation by Patrick K. O'Brien.
- PyDispatcher, the implementation on which the Django web framework's signals are based.
- Py-notify, a Python (plus a little C) implementation
- Observer Pattern using Weak References implementation by Michael Kent
- PyPubSub an in-application Pub/Sub library for Observer behavior
- NotificationFramework classes directly implementing Observer patterns
- Blinker, an implementation which can be used with decorators.
- Jpblib, an implementation that minimises boilerplate.
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
- Design Patterns (book), the book which gave rise to the study of design patterns in computer science
- Design pattern (computer science), a standard solution to common problems in software design
- Implicit invocation
- Model-view-controller (MVC)
- Client–server model
References
- http://www.research.ibm.com/designpatterns/example.htm
- http://msdn.microsoft.com/en-us/library/ms954621.aspx
- "Speaking on the Observer pattern" - JavaWorld