- Observer pattern
-
The observer pattern (a subset of the publish/subscribe 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. It is mainly used to implement distributed event handling systems.
Contents
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 amain()
method that might be used in order to run the code./* File Name : EventSource.java */ package 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 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 obs; public class MyApp { public static void main(String args[]) { System.out.println("Enter Text >"); // create an event source - reads from stdin final EventSource evSrc = new EventSource(); // create an observer final ResponseHandler respHandler = new ResponseHandler(); // subscribe the observer to the event source evSrc.addObserver( respHandler ); // starts the event thread Thread thread = new Thread(evSrc); 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
- NSKeyValueObserving - The Objective C NSKeyValueObserving protocol.
C#
- The IObserver<T> Interface - The .NET Framework 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
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
- JavaScript Observer Pattern. Pocket-sized minimalist framework of common design patterns in 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.
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
- Event_Dispatcher, a PHP implementation
- SPL, the Standard PHP Library
- Symfony Event Dispatcher, a standalone library by the Symfony team
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.
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. 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
- ^ Gang Of Four
- ^ java.util.Observer
- ^ Which will typically implement interface java.util.EventListener
- ^ http://download.oracle.com/javase/tutorial/uiswing/events/generalrules.html
- ^ Deprecating the Observer Pattern by Ingo Maier, Tiark Rompf, Martin Odersky (2010) PDF
- http://www.research.ibm.com/designpatterns/example.htm
- http://msdn.microsoft.com/en-us/library/ms954621.aspx
- "Speaking on the Observer pattern" - JavaWorld
External links
- Observer Pattern implementation in JDK 6
- Observer Pattern in Java
- Definition, C# example & UML diagram
- Jt J2EE Pattern Oriented Framework
- Subject Observer example in C++
- Observer Pattern recipe in Python
- SourceMaking Tutorial
- Observer Pattern in Objective-C
- Observer Pattern in Java (Portuguese)
Creational Structural Behavioral Categories:- Software design patterns
- Articles with example Java code
Wikimedia Foundation. 2010.