Observer pattern
|
|
This article includes a list of references, related reading or external links, but its sources remain unclear because it lacks inline citations. Please improve this article by introducing more precise 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.
Contents |
[edit] Structure
[edit] 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]
[edit] Example
| The Wikibook Computer Science Design Patterns has a page on the topic of |
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 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(); } }
[edit] 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:
[edit] ActionScript
- flash.events, a package in ActionScript 3.0 (following from the mx.events package in ActionScript 2.0).
[edit] C
- GObject, in GLib - an implementation of objects and signals/callbacks in C. (This library has many bindings to other programming languages.)
[edit] 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.
[edit] Objective C
- NSKeyValueObserving - The Objective C NSKeyValueObserving protocol.
[edit] 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
[edit] ColdFusion
- http://www.cfdesignpatterns.com/behavioral-patterns/observer-design-pattern-in-coldfusion/
- http://coldfusioneventmanager.riaforge.org/
[edit] Delphi
- Delphi Observer Pattern, a Delphi implementation
[edit] 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]
[edit] 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.
[edit] Lisp
- Cells, a dataflow extension to Common Lisp that uses meta-programming to hide some of the details of Observer pattern implementation.
[edit] Perl
- Class::Observable Basic observer pattern implementation
- Class::Publisher a slightly more advanced implementation
- POE::Component::Syndicator Observer pattern for POE
[edit] PHP
- Event_Dispatcher, a PHP implementation
- SPL, the Standard PHP Library
- Symfony Event Dispatcher, a standalone library by the Symfony team
[edit] 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.
[edit] Ruby
- Observer, from the Ruby Standard Library.
[edit] 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.
[edit] Critics
|
|
This article's Criticism or Controversy section may compromise the article's neutral point of view of the subject. Please integrate the section's contents into the article as a whole, or rewrite the material. (December 2011) |
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.
[edit] 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
[edit] References
- http://www.research.ibm.com/designpatterns/example.htm
- http://msdn.microsoft.com/en-us/library/ms954621.aspx
- "Speaking on the Observer pattern" - JavaWorld
[edit] External links
- Observer Pattern implementation in JDK 7
- Observer Pattern in Java
- Definition, C# example & UML diagram
- Subject Observer example in C++
- Observer Pattern recipe in Python
- SourceMaking Tutorial
- Observer Pattern in Objective-C
- Observer Pattern in Java (Portuguese)
|
|||||||||||