Observer pattern
|
The observer pattern is a design pattern used in computer programming to observe the state of an object in a program.
The essence of this pattern is that several objects (called listeners) are registered to hear for an event which may be raised by some object. (The object which may raise an event holds a list of the listeners.)
When the event is raised for each listener is called a some callback (which may be either a virtual method of the listeners class or a function pointer (more generally a functor) passed as an argument to the listener registration method).
Typical usages
The typical usages of the observer pattern:
- Listening for an external event (such as a user action). See Event-driven programming.
- Listening for changes of the value of a property of an object. Note that often callbacks called in response of a property value change also change values of some properties, so sometimes causing an event cascade. See this article (http://ex-code.com/articles/binding-properties.html) for a discussion about using observer pattern for watching over changes of properties and updating other properties accordingly.
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:
- libsigc++ (http://libsigc.sourceforge.net) - the C++ signalling template library.
- GLib (http://www.gtk.org) - an implementation of objects and signals/callbacks in C. (This library has many bindings to other programming languages.)
External link
http://www.ondotnet.com/pub/a/dotnet/2005/01/03/binaryclock.html