Google
 
   
Login
Username:

Password:


Lost Password?

Register now!
Search
Main Menu
top books
Polls
What do you think about php-deluxe.net?
Excellent!
Cool
Hmm..not bad
What the hell is this?
encyclopedia
recommendation
compare webbrowser
Freenet DSL
Who's Online
4 user(s) are online (3 user(s) are browsing encyclopedia)

Members: 0
Guests: 4

more...
browser tip
Unix Befehle
manual of unix befehle
recommendation!
Sponsored
partner

Observer pattern

The observer pattern (sometimes known as publish subscribe) is a design pattern (computer science) used in computer programming to observe the state of an Object (computer science) in a computer program.

The essence of this pattern is that one or more objects (called observers or listeners) are registered (or register themselves) to observe an Event-driven programming which may be raised by the observed object (the subject). (The object which may raise an event generally maintains a collection of the observers.)

The Unified_Modeling_Language diagram below illustrates this structure:

When the event is raised each observer receives a Callback (computer science) (which may be either a virtual function of the observer class (called notify on the diagram) or a function pointer (more generally a function object or functor ) passed as an argument to the listener registration method). The notify function may also be passed some parameters (generally information about the event that is occuring) which can be used by the observer.

Each concrete observer implements the notify function and as a consequence defines its own behavior when the notification occurs.

The subject normally has a register method for adding a new listener and an unregister method for removing an observer from the list of objects to be notified when the event is raised. It may also have methods for temporarily disabling and then reenabling calls to prevent inefficient cascading of a number of related updates. This is sometimes referred to as event compression.

In systems where the observer pattern is used extensively, a mechanism to prevent looping is usually implemented. Suppose that A updates B when event X is raised, and B subsequently updates A inside of the handler for event X in B in such a way that it causes A to raise event X again. It is desirable in such situations that A not raise event X again while event X is already in the process of being raised.

=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 [http://ex-code.com/articles/binding-properties.html this article] for a discussion about using observer pattern for watching over changes of properties and updating other properties accordingly.
  • The observer pattern is also very often associated with the MVC paradigm. In MVC, the observer pattern is used to create a loose coupling between the model and the view. Typically, a modification in the model triggers the notification of model observers which are actually the views.

    =Implementations=

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

    Some of the most notable implementations of this pattern:

  • The Java Java_Swing library makes extensive use of the observer pattern for event management
  • [http://libsigc.sourceforge.net libsigc++] - the C++ Signal programming template library.
  • [http://www.gtk.org GLib] - an implementation of objects and Signal programming/Callback (computer science) in C. (This library has many bindings to other programming languages.)
  • [http://msdn.microsoft.com/library/default.aspurl=/library/en-us/dnbda/html/observerpattern.asp Exploring the Observer Design Pattern] - the C Sharp (C#) and Visual Basic .NET implementation, using delegates and the Event pattern
  • =External links=

    *[http://www.ondotnet.com/pub/a/dotnet/2005/01/03/binaryclock.html Using the Observer Pattern in .NET]

    *[http://www.dofactory.com/Patterns/PatternObserver.aspx Definition & UML diagram]

    *[http://kelly.anderson.name/patterns/observer/earlyobserverpattern.htm A discussion of fine-grained observers]