Observer

From CSSEMediaWiki
Revision as of 02:43, 23 July 2009 by Michal Connole (Talk | contribs)
Jump to: navigation, search

The Observer design pattern is used to observe the state of an object. Whenever the state of an object (subject) changes, all depending objects (observer) are notified and updated automatically. Therefore the subject should know about all its dependent observers and keep a list of them. The subject has a one-to-many dependency to all observer. Every observer has to register with subject to be able to get notification on subject state changes.

Contents

Typical Usage

  • When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects allows the programmer to vary and reuse them independently.
  • When a change to one object requires changing others and it is not known in advance how many objects need to be changed.
  • When an object should be able to notify others but shouldn't know anything about them

One or more than one component display in a graphical way the state of an object. Changes the state of the object, all components need to be informed. All components know about the object but the object should stay independent from the components. Therefore it should be a loose coupling. In this case you would use the observer pattern.

Example: Results of a measurement are illustrated in a bar chart, line chart and in a table. The measured values are permantly changing. The components should always display those changes but the measured object itself shouldn't need to know anything about the structure of those components.

Structure

From the Gang of Four Book:

Observer.jpg

Participant Classes

Subject

This abstract class provides an interface for attaching and detaching observer objects and notifying those.

  • registerObserver(): adds new observer to the list of observers
  • removeObserver(): removes a certain observer from the list of observers
  • notifyObserver(): notifies each observer that the state of the subject changed by going through the list of observers

ConcreteSubject

This class returns its own state whenever asked by the observer objects. It also calls up its superclass' notifyObserver() function

  • getState(): returns the state of the subject

Observer

This class defines an update interface for all observer objects.

  • update(): abstract method.

ConcreteObserver

This class maintains a reference to the observing subject.

  • update(): When this function is called by the subject, the observer calls the getState() function to update its information about the subject's state

Recognising the pattern

Classes: Observer, multiple ConcreteObservers, Subject, ConcreteSubject.

  • Observer class that implments java.util.Observer in Java.
  • Subject class that extends java.util.Observable in Java.

OR

  • addObserver(Observer) and removeObserver(Observer) methods in Subject.
  • notifyAll() method in Subject.
  • abstract update() method in Observer.

See also


Personal tools