Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Observer and Observable

An introduction to the Observer interface and Observable class using the Model/View/Controller architecture as a guide

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 5

Defining the parts

The model is the object that represents the data in the program. It manages the data and conducts all transformations on that data. The model has no specific knowledge of either its controllers or its views -- it contains no internal references to either. Rather, the system itself takes on the responsibility of maintaining links between the model and its views and notifying the views when the model changes.

The view is the object that manages the visual display of the data represented by the model. It produces the visual representation of the model object and displays the data to the user. It interacts with the model via a reference to the model object itself.

The controller is the object that provides the means for user interaction with the data represented by the model. It provides the means by which changes are made, either to the information in the model or to the appearance of the view. It interacts with the model via a reference to the model object itself.

At this point a concrete example might be helpful. Consider as an example the system described in the introduction.



3D Visualization System



Figure 2: Three-dimensional visualization system

The central piece of the system is the model of the three-dimensional scene. The model is a mathematical description of the vertices and the faces that make up the scene. The data describing each vertex or face can be modified (perhaps as the result of user input or a scene distortion or morphing algorithm). However, there is no notion of point of view, method of display (wireframe or solid), perspective, or light source. The model is a pure representation of the elements that make up the scene.

The portion of the program that transforms the data in the model into a graphical display is the view. The view embodies the actual display of the scene. It is the graphical representation of the scene from a particular point of view, under particular lighting conditions.

The controller knows what can be done to the model, and implements the user interface that allows that action to be initiated. In this example, a data entry control panel might allow the user to add, modify, or delete vertices and faces.

Observer and Observable

The Java programming language provides support for the Model/View/Controller architecture with two classes:

  • Observer -- any object that wishes to be notified when the state of another object changes

  • Observable -- any object whose state may be of interest, and in whom another object may register an interest



These two classes can be used to implement much more than just the Model/View/Controller architecture. They are suitable for any system wherein objects need to be automatically notified of changes that occur in other objects.

Typically, the model is a subtype of Observable and the view is a subtype of observer. These two classes handle the automatic notification function of the Model/View/Controller architecture. They provide the mechanism by which the views can be automatically notified of changes in the model. Object references to the model in both the controller and the view allow access to data in the model.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (3)
Login
Forgot your account info?

Very helpful, thanks!By Anonymous on July 20, 2009, 1:52 pmVery helpful, thanks!

Reply | Read entire comment

GreatBy Anonymous on July 2, 2009, 12:23 amIt gives a very good description. but i wanted some implementation code also

Reply | Read entire comment

ExcelentBy Anonymous on January 30, 2009, 2:00 amI got very clear idea onthis topic after reading this

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources