Observer

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(New page: 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 automati...)
 
Line 1: Line 1:
 
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-may dependency to all observer. Every observer has to register with subject to be able to get notification on subject state changes.
 
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-may dependency to all observer. Every observer has to register with subject to be able to get notification on subject state changes.
 +
 +
==Participant Classes==
  
 
===Subject===
 
===Subject===
Line 14: Line 16:
  
 
===Observer===
 
===Observer===
This class defines an update interface for all (concrete) observer objects.
+
This class defines an update interface for all observer objects.
 +
 
 
*update():
 
*update():
  
 
===ConcreteObserver===
 
===ConcreteObserver===
This class keeps a reference to the (concrete) subject
+
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
 
*update(): When this function is called by the subject, the observer calls the getState() function to update its information about the subject's state
 +
 +
==Typical Usage==
 +
 +
==See also==
 +
*[[Model-View-Control]]

Revision as of 21:57, 2 September 2008

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-may dependency to all observer. Every observer has to register with subject to be able to get notification on subject state changes.

Contents

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 return 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():

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

Typical Usage

See also

Personal tools