Observer-Observable Design Pattern

COMP 310  Java Resources  Eclipse Resources

(Back to Design Patterns)

Summary: The Observer-Observable enables centralized, transparent, one-way communications to a set of decoupled objects.

The Observer-Observable design pattern is a a very useful pattern for maintaining one-way communciation between one object and a set of other objects. The pattern consists of

  • Observers: These objects receive communications from the Observable. Observers do not have a reference back to the Observable however. Thus the communication is strictly from the Observable to the Observers.
  • Observable: This is the object the Observers are "watching". The Observable maintains some sort of list of references to Observers. That way, the Observable can send a message to all the Observers by calling each of their update methods.

If the state of the Observable changes, the Observers need to be notified. This need not happen exactly when the Observable changes state, but rather when the Observable is asked to update the Observers. Thus the Observables, "update" method may not necessarily pass a message to the Observers if the Observable's state has not changed.

The update methods of both the Observable and Observers usually takes and argument so that data can be passed with the update command.

A common way to use the Observer-Observable pattern is to assume the state of the Observable has always changed when its update method is called. This is more descriptively a "dispatcher" pattern. In this situation, the Observers are always notified via their update methods whenever the Observable's update method is called.

Observer-Observable Pattern

 

 

 


Originally published in Connexions (CNX): https://web.archive.org/web/20130325222414/https://cnx.org/content/m26416/latest/

© 2023 by Stephen Wong